mysql 排序位置,mysql排序表并获取行位置

I've got a problem in mysql, I have a table of ID and nubmer ... somthing like that

ID -------- number

3 -------- 340

1 -------- 10

12 -------- 23

And now I would like to selecet by ID, let's say ID=3 and is there a way of saying what is the position of this row in sorted table? In this case, ID = 3 would have first position, cuase of highest value in number. ID=12 would have second position ... and so on.

解决方案set @row_number:=0;

select * from

(select ID, @row_number:=@row_number+1 from your_table order by number desc)

as row_to_return

where ID=3;

The above query can be change to replace ID to anything you need.

However, for simple usage, ORDER BY number DESC is better and optimized.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值