I'm working on some code in php/MySql and I need to update a field called scorerank based on the score value. I found a previous question here that addressed what I was looking for:
Update the rank in a MySQL Table
I modified their example to work with my database. I also added a condition so that I only modified columns that had a specific tourneyid and tableid. I used a unique index variable in the inner join to id which row to update - I think I did it right, but I get the following error:
1248 - Every derived table must have its own alias
Here's my code. Can anybody spot the error(s)?
UPDATE tscores
JOIN (SELECT p.userid,p.scoreid,
IF(@lastPoint <> p.score,
@curRank := @curRank + 1,
@curRank) AS rank,
IF(@lastPoint = p.score,
@curRank := @curRank + 1,
@curRank),
@lastPoint := p.score
FROM tscores p
WHERE (p.tourneyid=1 and p.tableid=2)
JOIN (SELECT @curRank := 0, @lastPoint := 0)
ORDER BY p.score DESC
) ranks ON (ranks.userid = tscores.userid)
SET tscores.scorerank = ranks.scorerank
WHERE tscores.scoreid = p.scoreid
Here's the table I'm working with:
CREATE TABLE IF NOT EXISTS `tscores` (
`scoreid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tourneyid` int(10) unsigned NOT NULL,
`tableid` int(10) unsigned NOT NULL,
`userid` int(10) unsigned NOT NULL,
`score` int(16) unsigned NOT NULL DEFAULT '0',
`scorestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`scoreround` int(3) unsigned NOT NULL DEFAULT '0',
`scoregroup` int(3) unsigned NOT NULL DEFAULT '0',
`scorerank` int(4) unsigned DEFAULT NULL,
PRIMARY KEY (`scoreid`),
KEY `tourneyid` (`tourneyid`,`tableid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=62 ;