MySQL: Getting a row number (ranking) for a specific row

前端 未结 3 436
刺人心
刺人心 2020-12-31 21:31

I have a users table that has a column called money_sent. I want to order this table by money_sent in descending order, and then find

相关标签:
3条回答
  • 2020-12-31 21:57

    How about:

    SELECT count(*) FROM users WHERE money_sent < (
        SELECT money_sent FROM users WHERE user = 'joe'
    );
    
    0 讨论(0)
  • 2020-12-31 21:59

    If you also want to get the user's row along with that user's rank, you can use something like this:

    SELECT u1.*, COUNT(u2.user) 
    FROM users u1
      LEFT OUTER JOIN users as u2 ON (u1.money_sent < u2.money_sent)
    GROUP BY u1.user;
    
    0 讨论(0)
  • 2020-12-31 22:01
    SELECT Row,user, money_sent
    FROM (SELECT @row := @row + 1 AS Row, user, money_sent 
           FROM table1 order by money_sent desc) 
    As derived1 
    
    0 讨论(0)
提交回复
热议问题