MYSQL目前不支持行号功能,如果想按某字段进行排序,然后得到排序号,很麻烦,要想实现这种功能
解决方法是通过预定义用户变量来实现:
set @mycnt = 0;
select * from (
select (@mycnt := @mycnt 1) as ROWNUM , othercol
from tblname order by othercol
) as A where othercol=OneKeyID;
实例:
表 a:
UID Money
2 444
1 222
3 555
4 6666
想要以Money排序取得排行号:SQL文如下:
Select UID,(@rowNum:=@rowNum+1) as rowNo
From a,
(Select (@rowNum :=0) ) b
Order by a.Money Desc
输入结果如下:
UID rowNo
4 1
3 2
2 3
1 4
转载于:https://blog.51cto.com/happyliu/1582423