我正在寻找一种方法来选择出现次数最多的值,例如在每个主题中发帖最多的人;
SELECT MOST_OCCURRING(user_id) FROM thread_posts GROUP BY thread_id
有什么好的方法吗?
最佳答案
如果你想在每个线程的基础上进行计数,我认为你可以使用嵌套查询;先按线程分组,再按用户分组:
SELECT thread_id AS tid,
(SELECT user_id FROM thread_posts
WHERE thread_id = tid
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 0,1) AS topUser
FROM thread_posts
GROUP BY thread_id
关于mysql - 选择 MySQL 中出现次数最多的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3257064/