简体   繁体   中英

How do I exclude infrequent usernames from mysql query?

I have this code

SELECT usernames FROM table1

I want to select every username from table1 however I want to exclude usernames that appear less then 5 times in table2 .

So I want this: Select all usernames from table1, but exclude the ones that appear in less then 5 rows in table2.

How can I do that?

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
select username from table1 where username in
(
select username from (
select  username ,
count( * )
from table2 
group by username
having count( * ) > 5
) as x
);

tested on mysql 5.6 : schema and code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM