MySQL选择前10个最重复的字段值

时间:2014-05-19 22:56:51

标签: mysql

我需要查询我的数据库以返回列中前10个最常见的值。我现在正在使用它:

$most_popular = (' 
SELECT item_id 
FROM   active_members 
WHERE  component = "members" 
       AND type = "has_picture" 
GROUP  BY item_id 
HAVING Count(*) = (SELECT Count(*) 
                   FROM   wp_ms_activity 
                   GROUP  BY item_id 
                   ORDER  BY Count(*) DESC 
                   LIMIT  1)

 ' );

这只会返回一个值,而且速度很慢。有没有更好的办法?我尝试将限制1更改为10,并且它说“子查询返回多个值”。

1 个答案:

答案 0 :(得分:1)

按每个item_id

的记录计数排序
SELECT item_id 
FROM active_members 
WHERE component = "members" 
AND type = "has_picture" 
GROUP BY item_id 
order by count(*) desc
limit 10