我想顯示的帖子最多的用戶。通過計算其用戶名在數據庫中顯示的次數來添加帖子。我如何獲取所有元素並檢查哪些值比其他值更顯着?我可以在mysql數據庫中獲得最高的值嗎?
所以說,我的數據庫是這樣的:
id | username
1 | test
2 | test
3 | no test
「測試」顯示在最,所以我怎麼可能說
highest poster: "test"
我想顯示的帖子最多的用戶。通過計算其用戶名在數據庫中顯示的次數來添加帖子。我如何獲取所有元素並檢查哪些值比其他值更顯着?我可以在mysql數據庫中獲得最高的值嗎?
所以說,我的數據庫是這樣的:
id | username
1 | test
2 | test
3 | no test
「測試」顯示在最,所以我怎麼可能說
highest poster: "test"
該查詢返回username
和出現次數,在整理倒序,所以第一條記錄是發生次數最多的一條:
select username, count(id) from tablename
group by username
order by count(id) desc
U PDATE: 正如指出的thedugas和喬·菲利普斯,你可以添加一個limit 1
子句此查詢來獲取只出現次數最多的紀錄
select username, count(id) as uc
from tableName
group by username
order by uc desc
limit 1
SELECT username
FROM mytable
GROUP BY username
ORDER BY COUNT(1) DESC
LIMIT 1