MySQL社区

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 1873|回复: 1
打印 上一主题 下一主题

[SQL类] MYSQL 排名

[复制链接]
跳转到指定楼层
1#
发表于 2013-2-6 17:50:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
I hava a table like this:
id,name,score,rank
1,anna,80,0
2,qera,65,0
6,kero,90,0
10,rosa,95,0
what I would like to do is to update the rank position.  I have this,
update mytable set rank= 1 where max(score);
update mytable set rank= 2 where max(score-1) ;
....
....
the result should be like this:
id,name,score,rank
1,anna,80,3
2,qera,65,4
6,kero,90,2
10,rosa,95,1
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 分享淘帖 顶 踩
2#
发表于 2013-7-14 12:35:37 | 只看该作者
if your table is named test1 ,then
update test1 x
inner join ( SELECT
a.id,count(*) as rank from  test1   a
inner join  test1 b on a.score<=b.score
group by  a.id) y
on x.id =y.id
set x.rank =y.rank
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|小黑屋|Archiver|手机版|MySQL社区 ( 京ICP备07012489号   
联系人:周生; 联系电话:13911732319

GMT+8, 2024-5-4 14:33 , Processed in 0.140484 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表