如何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;
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;