查找mysql表中插入次数最多的记录


Find the record which is inserted the most in mysql table

我有一个包含用户id,用户收入等的表。对于一个特定的用户id,表中可能包含两个以上的插入。我的任务是获取在表中插入次数最多的用户id。

试试这个

SELECT user_id 
FROM users 
group by user_id 
order by count(user_id) DESC
LIMIT 0,1

可以。

select user_id, count(*) from whatever_table group by user_id;

可能也想尝试在那里添加一些排序,但这是要点。

为什么不查找每个用户id的计数?然后,您可以对结果进行排序、追加或执行任何您想要的操作。

只是我的2美分。

使用排序和分组进行计数。

select user_id, count(*) as count from
whatever_table group by user_id order
by count DESC limit 1;

DESC使其从最高的数字开始并向后移动。limit语句使得只返回1条记录。