[英]Get user with highest number of records
I have a table "Records" in my database and a column "username" where I have stored the name of the user that has the record. 我在数据库中有一个表“ Records”,在其中存储了具有该记录的用户名的“ username”列中。 I want to get the user with the highest number of records. 我想让用户拥有最多的记录数。 The only idea that I have is to build a method to get the number of records for each user and then to find the greatest number among them. 我唯一的想法是建立一种方法来获取每个用户的记录数,然后在其中找到最大的记录数。 Is there any SQL query to do this or a simpler method? 是否有任何SQL查询可以执行此操作或更简单的方法? Thank you. 谢谢。
select username, count(*)
from Records
group by username
order by count(*) desc
limit 1
select username, count(*) As no_of_records
from Records
group by username
order by no_of_records desc
The above query will give all the rows with descending order of no. 上面的查询将给所有行以降序排列。 of records for users and for any number of limits you can use LIMIT
keyword followed by a number 用户记录数和任意数量的限制,您可以使用LIMIT
关键字后跟一个数字
for. 对于。 eg for highest LIMIT 1
例如最高LIMIT 1
for top 2 LIMIT 2
etc. 前2个LIMIT 2
等
Another way 其他方式
select username
from records
having count(*) =
(SELECT max(count(*)) FROM records group by username)
group by username
try this 尝试这个
SELECT username, sum(record) total_record
FROM Records
GROUP BY username
ORDER BY count(*) desc
EDIT : 编辑:
** if you want get just the first highest , do LIMIT 1 **如果您只想获得第一高,请执行LIMIT 1
select username, sum(record) total_record
from Records
group by username
order by count(*) desc
limit 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.