如何从mysql查询中排除不常用的用户名?

时间:2015-04-03 10:43:57

标签: mysql sql

我有这段代码

SELECT usernames FROM table1

我想从username中选择每个table1,但我想排除在usernames中出现少于5次的table2

所以我想要这样:从table1中选择所有用户名,但不包括table2中少于5行的用户名。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

select table1.username 
from table1 
inner join (select username, 
count(*) as count from table2 
group by username 
having count > 5) as tmp 
on table1.username = tmp.username

答案 1 :(得分:0)

select username from table1 where username in
(
select username from (
select  username ,
count( * )
from table2 
group by username
having count( * ) > 5
) as x
);

在mysql 5.6上测试:schema and code