update winners
set rank = (
select count(score) + 1
from winners w2
where w2.category_id = winners.category_id and w2.score > winners.score
)
count(*)
vil evaluere til nul, selv når ingen rækker matcher betingelsen, dvs. topplaceringen. Hvis du ville have en tæt rang, kunne du gøre dette:
update winners
set rank = (
select count(distinct score) + 1
from winners w2
where w2.category_id = winners.category_id and w2.score > winners.score
)
EDIT:I henhold til din kommentar har jeg fundet en forespørgsel, der virker. (Det virker på SQL Server, og jeg er ikke bekendt med MySQL's særheder.) http:// sqlfiddle.com/#!9/1159f/1
update winners
inner join (
select w.id, w.category_id, count(w2.score) + 1 rank
from winners w left outer join winners w2
on w2.category_id = w.category_id and w2.score > w.score
group by w.id
) r
on r.id = winners.id
set winners.rank = r.rank