答案 0 :(得分:2)
一种方法是进行GROUP BY
聚合,然后应用LIMIT
技巧:
SELECT user_id, COUNT(*) AS cnt
FROM yourTable
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1;
如果你想要更复杂的东西,那么你将进入等级功能领域。 MySQL(至少在当前版本中)不支持内置排名支持,因此执行此类查询可能很棘手。
答案 1 :(得分:1)
SELECT top 1 user_id, COUNT(*) AS cnt
FROM yourTable
GROUP BY user_id
ORDER BY COUNT(*) DESC
答案 2 :(得分:1)
有一个公用表表达式,用于计算每个user_id。选择user_id,其中count是最大计数。如果出现平局,将返回两个user_id。
with cte as
(
SELECT user_id, COUNT(*) AS cnt
FROM yourTable
GROUP BY user_id
)
select user_id
from cte
where cnt = (select max(cnt) from cte)
答案 3 :(得分:0)
Pure SQL:
select user_id, count(*)
from users
group by user_id
having count(*) = (select max(a.rows)
from (select user_id, count(*) as rows
from users
group by user_id
) a
)