我需要查询我的数据库以返回列中前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,并且它说“子查询返回多个值”。
答案 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