简体   繁体   English

MYSQL position 按顺序排列

[英]MYSQL position of row by order

I have had a look through google results, but can't find a simple answer, I have a table我查看了谷歌结果,但找不到简单的答案,我有一张桌子

id, points (and more fields, but they dont affect anything) id、points(和更多字段,但它们不影响任何东西)

How would I go about Getting position of record 24(id) ordered by points DESC?我将如何 go 关于获取由点 DESC 排序的记录 24(id) 的 position?

I'm not sure I've understood your question我不确定我是否理解了你的问题

select * from table order by points desc limit 23,1
Select Count(*) from (
Select ID from UnNamedTable A where A.points>(Select B.Points from UnNamedTable B where B.ID=24)
)

Are you trying to figure out out what place a user is in based on a points system?您是否试图根据积分系统找出用户所在的位置? If so then just find how many users have more points than a particular user then add 1.如果是这样,那么只需找出有多少用户比特定用户拥有更多积分,然后加 1。

SELECT COUNT(*) + 1 AS 'position' FROM table WHERE points > 
   ( SELECT points FROM table WHERE id = someIDhere )

You could create a variable to keep track of your row position after sorting.您可以在排序后创建一个变量来跟踪您的行 position。 Something like this would work:像这样的东西会起作用:

select @rownum:=@rownum+1 ‘rank’, p.* from table1 p, (SELECT @rownum:=0) ID order by Points desc limit 10;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM