mysql 排序后获得某行的位置

假设有test表,下图为表机构和数据,score表示积分。现在要查询积分排名为第几的id??

查询语句

[sql]  view plain copy
  1. select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc;  

查询结果:

这样就得到了根据积分排名的位置了(rowno),要获得id只要在这个表的基础上进行查询就可以了

[sql]  view plain copy
  1. select id from (select id,score,(@rowno:=@rowno+1) as rowno from test,(select (@rowno:=0)) b order by score desc) c where rowno=1;  
rowno的值为你想要查询的名次,就得到对应的id了。

上面的rowno为1,排名第一的id就是2了



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值