简体   繁体   中英

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)

How would I go about Getting position of record 24(id) ordered by points DESC?

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.

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. Something like this would work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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