获取最高分的用户
Get user with highest points
我想获得 SQL 中得分最高的用户。我有这个 Table:
userId
points
1
1
2
0
3
4
1
1
3
2
2
5
现在,我想获取积分最高的userId?在此示例中,它位于用户 3 的位置,但我如何自动执行此操作是 SQL?
我想我有适合您的解决方案,请查看下面的代码=>
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);
此 ss MYSQL 8.0
WITH CTE AS
(SELECT USERID,
points,
RANK() OVER(ORDER BY points DESC) RNK
FROM USERS
)
SELECT * FROM CTE
WHERE RNK=1
注意: 检查 db-fiddle 中的输出。
我想获得 SQL 中得分最高的用户。我有这个 Table:
userId | points |
---|---|
1 | 1 |
2 | 0 |
3 | 4 |
1 | 1 |
3 | 2 |
2 | 5 |
现在,我想获取积分最高的userId?在此示例中,它位于用户 3 的位置,但我如何自动执行此操作是 SQL?
我想我有适合您的解决方案,请查看下面的代码=>
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);
此 ss MYSQL 8.0
WITH CTE AS
(SELECT USERID,
points,
RANK() OVER(ORDER BY points DESC) RNK
FROM USERS
)
SELECT * FROM CTE
WHERE RNK=1
注意: 检查 db-fiddle 中的输出。