我有两个mysql表,我想从另一个表中选择那些计数大于2的ID。
这是我的问题:
SELECT * FROM `firsttable` where from_user_id in (SELECT target,count(*) as c FROM `second table`where type='follower' group by target having c>2)
但是我遇到操作数应该包含1个列的错误(同样,每当我从xy中选择z(in ...)查询时,查询似乎需要很长时间。)
答案 0 :(得分:2)
in
运算符仅适用于单列查询。幸运的是,您不必在选择列表中包含count
,以便将其包含在having
子句中:
SELECT *
FROM `firsttable`
WHERE from_user_id IN (SELECT target
FROM `second table`
WHERE type =' follower'
GROUP BY target
HAVING COUNT(*) > 2)