如何在MySQL表的一栏中找到最常见的结果 [英] How do I find the most common result in a column in my MySQL table
问题描述
我要使用PHP和MySQL查询用户发布的帖子表,以找到发布最多帖子的人.
Using PHP and MySQL, I want to query a table of postings my users have made to find the person who has posted the most entries.
对此的正确查询是什么?
What would be the correct query for this?
示例表结构:
[id] [UserID]
1 johnnietheblack
2 johnnietheblack
3 dannyrottenegg
4 marywhite
5 marywhite
6 johnnietheblack
我希望看到约翰尼·布莱克"是头号海报,玛丽·怀特"是次之,而丹尼·罗特尼格"则是最少的
I would like to see that "johnnietheblack" is the top poster, "marywhite" is second to best, and "dannyrottenegg" has the least
推荐答案
类似的东西
SELECT COUNT(*) AS `Rows`, UserID
FROM `postings`
GROUP BY UserID
ORDER BY `Rows` DESC
LIMIT 1
这将获取由特定ID发布的行数,然后对计数进行排序以找到最大值,然后输出该最大值以及此人的ID.不过,您需要将"UserID"和"postings"替换为适当的列和字段.
This gets the number of rows posted by a particular ID, then sorts though the count to find the highest value, outputting it, and the ID of the person. You'll need to replace the 'UserID' and 'postings' with the appropriate column and field though.
这篇关于如何在MySQL表的一栏中找到最常见的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!