我有一个表,其中的数据如下
ID Classroom Person
1 1 Alfred
2 1 Maria
3 2 Maria
4 2 Zoe
5 2 Alfred
6 3 Nick
7 3 Paul
8 3 Mike
9 3 Alfred
10 4 Zoe
11 4 Maria
我只想选择并返回只有“阿尔弗雷德”和“玛丽亚”这样一个人的教室:
Select * from table_name where (Person='maria') and (Person=Alfred')
似乎不起作用。
你叫在这里看到一个,
发布于 2014-12-30 17:01:30
您可以使用group by
和having
select classroom
from table t
group by classroom
having count(*) = 2 and
sum(person in ('maria', 'Alfred')) = 2;
这假设一个人不能多次出现在教室里。
这检查了教室里有两个名字,他们是两个感兴趣的名字。如果您可以拥有副本,则需要:
having count(distinct name) = 2 and
count(distinct case when person in ('maria', 'Alfred') then person end) = 2;
发布于 2014-12-30 17:00:25
尝尝这个。Group by and having
与Count
一起工作。
SELECT Classroom
FROM tablename
WHERE Person IN( 'maria', 'Alfred' )
GROUP BY classroom
HAVING Count(Person) = 2
https://stackoverflow.com/questions/27709399
复制相似问题