我正在寻找一个查询,只得到最大的查询。
SELECT results.playerid,
username,
Count(*) AS count
FROM results
LEFT JOIN players
ON results.playerid = players.playerid
WHERE points = 5
GROUP BY playerid
ORDER BY count DESC
结果:
+-----------+-----------+-------+
| playerID | username | count |
+-----------+-----------+-------+
| 1 | Lorem | 2 |
| 2 | Ipsum | 2 |
| 3 | Dolor | 1 |
+-----------+-----------+-------+
预期输出:
+-----------+-----------+-------+
| playerID | username | count |
+-----------+-----------+-------+
| 1 | Lorem | 2 |
| 2 | Ipsum | 2 |
+-----------+-----------+-------+
如果只有最大的一个,那就没问题了。
有什么提示吗?
答案 0 :(得分:-1)
首先,您需要找出group by
SELECT Count(players.playerid) AS count
FROM results
LEFT JOIN players
ON results.playerid = players.playerid
WHERE points = 5
GROUP BY players.playerid
ORDER BY count DESC
LIMIT 1;
然后您使用该查询过滤所需的结果。
SELECT results.playerid,
username,
Count(players.playerid ) AS count
FROM results
LEFT JOIN players
ON results.playerid = players.playerid
WHERE points = 5
GROUP BY playerid
HAVING COUNT(players.playerid) = (
SELECT Count(players.playerid) AS count
FROM results
LEFT JOIN players
ON results.playerid = players.playerid
WHERE points = 5
GROUP BY players.playerid
ORDER BY count DESC
LIMIT 1
);
输出
| playerid | username | count |
|----------|----------|-------|
| 1 | Lorem | 2 |
| 2 | Ipsum | 2 |