【问题标题】:Get user with highest points [duplicate]获得最高分的用户[重复]
【发布时间】:2021-02-13 16:04:53
【问题描述】:

我想获取 SQL 中得分最高的用户。我有这张桌子:

userId points
1 1
2 0
3 4
1 1
3 2
2 5

现在,我想获取分数最高的 userId?在这个例子中,它是用户 3,但我如何自动执行此操作是 SQL?

【问题讨论】:

  • 提示:GROUP BY。你有没有尝试过?

标签: mysql sql mariadb highest


【解决方案1】:

我想我有一个解决方案,请检查下面的代码=>

CREATE TABLE USERS (userId int,points int);

INSERT INTO USERS VALUES(1,1);
INSERT INTO USERS VALUES(2,0);
INSERT INTO USERS VALUES(3,4);
INSERT INTO USERS VALUES(1,1);
INSERT INTO USERS VALUES(3,2);
INSERT INTO USERS VALUES(2,5);
INSERT INTO USERS VALUES(1,5);

这是 MYSQL 8.0 的 ss

   WITH CTE AS
    (SELECT USERID,
           points,
           RANK() OVER(ORDER BY points DESC) RNK
    FROM USERS 
    )
    SELECT * FROM CTE
    WHERE RNK=1

注意:检查db-fiddle中的输出。

【讨论】:

  • 感谢 Awnser!但是有没有办法在 Mariadb 10x 中做到这一点?
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
相关资源
最近更新 更多