我可以获得mysql数据库中某个值的最高数量吗?

发布于 2024-10-28 23:00:26 字数 234 浏览 3 评论 0原文

我想显示帖子最多的用户。通过计算用户名在数据库中显示的次数来添加帖子。如何获取所有元素并检查哪个值比其他值出现得更多?

所以说我的数据库看起来像这样:

id | username
1 | test
2 | test
3 | no test

“测试”显示最多,所以我怎么说

highest poster: "test"

I want to display the user with the most posts. the posts are added by counting how many times their username is displayed in the database. How can I grab all the elements and check to see which value appears more then others?

So say my database looks like this:

id | username
1 | test
2 | test
3 | no test

"test" is shown the most, so how could I say

highest poster: "test"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扬花落满肩 2024-11-04 23:00:26

此查询返回用户名和出现次数,按相反顺序排序,因此第一个记录是出现次数较多的记录:

select username, count(id) from tablename 
group by username
order by count(id) desc

更新:
正如 thedugasJoe Phillips 所指出的,您可以向此查询添加一个 limit 1 子句,以仅获取出现次数最多的记录

This query returns username and number of occurrences, sorted in reverse order, so the first record is the one with more occurrences:

select username, count(id) from tablename 
group by username
order by count(id) desc

UPDATE:
As pointed by thedugas and Joe Phillips, you can add a limit 1 clause to this query to get only the record with the highest number of occurrences

独﹏钓一江月 2024-11-04 23:00:26
select username, count(id) as uc
from tableName
group by username
order by uc desc
limit 1
select username, count(id) as uc
from tableName
group by username
order by uc desc
limit 1
夜清冷一曲。 2024-11-04 23:00:26
SELECT username
FROM mytable
GROUP BY username
ORDER BY COUNT(1) DESC
LIMIT 1
SELECT username
FROM mytable
GROUP BY username
ORDER BY COUNT(1) DESC
LIMIT 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文