我有这个查询
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
所以ids重复
答案 0 :(得分:1)
您可以选择最高分数并按用户分组。
SELECT u.fbid, u.name, MAX(r.points) max_points
FROM users u, player_records r
WHERE u.fbid = r.user_id
GROUP BY u.fbid, u.name
ORDER BY max_points DESC LIMIT 5
答案 1 :(得分:1)
听起来你想要每个用户的最大点数。所以你可以这样做:
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
答案 2 :(得分:1)
如果你需要前五名球员:
SELECT TOP 5 u.fbid, u.name, max(r.points) AS points
FROM users u
LEFT JOIN players_records r ON u.fbid = r.user_id
GROUP BY u.fbid, u.name
ORDER BY points DESC
如果你需要按SUM积分排名前5位球员:
SELECT TOP 5 u.fbid, u.name, SUM(r.points) AS points
FROM users u
LEFT JOIN players_records r ON u.fbid = r.user_id
GROUP BY u.fbid, u.name
ORDER BY points DESC
答案 3 :(得分:0)
这样的事情可能与你想要的一样。
select fbid, u.name, sum(r.points) totalpoints
from users u join players_records r on u.fbid = r.user_id
group by fbid, u.name
order by totalpoints desc
limit 5