![](/img/trans.png)
[英]to write a select query which strictly satisfies a multiple condition on same column
[英]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.