gpt4 book ai didi

mysql - DISTINCT 不在内连接中工作

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:04 24 4
gpt4 key购买 nike

我有这个问题

SELECT DISTINCT u.fbid, u.name,r.points
FROM users u, players_records r
WHERE u.fbid = r.user_id
ORDER BY r.points DESC LIMIT 5

我想获得顶级玩家,但只有 5 个不同的玩家,此查询不起作用它显示重复的用户 ID有什么帮助吗?

上述查询的结果

1112222 Name 1 9310
3334444 Name 2 8380
3334444 Name 2 7010
5555666 Name 3 6080
1112222 Name 1 4890

所以id是重复的

最佳答案

听起来您想要每个用户的最大积分。所以你可以这样做:

SELECT
users.fbid,
users.name,
maxRecords.points
FROM
users
JOIN
(
SELECT
MAX(players_records.points) AS points,
players_records.user_id
FROM
players_records
GROUP BY
players_records.user_id
) AS maxRecords
ON maxRecords.user_id=users.fbid
ORDER BY
maxRecords.points DESC
LIMIT 5

如果我了解你的数据。然后输出将是这样的:

1112222 Name 1 9310
3334444 Name 2 8380
5555666 Name 3 6080

关于mysql - DISTINCT 不在内连接中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26038009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com