繁体   English   中英

如何 select 满足 SQL 中某些条件的同一用户的多行?

[英]How to select multiple rows of the same user that satisfies some condition in SQL?

A有以下数据

id user_id visited_country
1  12      Spain 
2  12      France
3  14      England
4  14      France
5  16      Canada
6  14      Spain 

我想要 select 所有访问过西班牙法国的用户。 我怎样才能在 MySQL 中做到这一点?

像下面这样的东西就足够了:

select user_Id
from t
where visited_country in ('Spain','France')
group by User_Id
having Count(distinct visited_country) = 2;

@Aman:如果你想排除英格兰,那就是

select distinct user_Id
from table
where visited_country in ('Spain','France')
and not exists
(Select distinct user_id
 From table where visited_country in ('England'))
group by User_Id
having Count(distinct visited_country)> = 2;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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