这个怎么样:
update mytable set rating_place =(select count(*)+1 from mytable intb where intb.rating_score>mytable.rating_score)
----编辑(评论后)aww抱歉,你不能从你在mysql中更新的同一个表中选择,所以用一个临时表试试:
create table mytemptable as
select @row := @row +1 as place, mytable.id
from mytable, (SELECT @row := 0) r
order by rating_score desc;
然后只是一个类似的更新:
update mytable set rating_place = (
select place
from mytemptable
where mytemptable.id=mytable.id
)
之后你可以放弃那个 mytemptable。
虽然如果你想避免单独的表并且你可以使用php,你可以尝试
$res=mysql_query("select id from mytable order by rating_score desc");
$ratings=array();
while ($r=mysql_fetch_assoc($res)) {
$ratings[]=$r['id'];
}
foreach ($ratings as $key=>$val) {
mysql_query("update mytable set rating_score=".($key+1)." where id=".$val);
}