有趣的SQL问题

TRV*_*SQL 2 sql t-sql sql-server-2005

我有一个SQL问题,我试图消化.我正在使用SQL Server 2005.

在表格中我有这样的数据:

ID     Type
1        A
2        A
3        A
3        B
4        B
Run Code Online (Sandbox Code Playgroud)

我需要找到所有类型都是A和B的ID.

Mar*_*ers 12

使用INTERSECT运算符:

   SELECT DISTINCT ID FROM [Table] WHERE Type = 'A'
   INTERSECT
   SELECT DISTINCT ID FROM [Table] WHERE Type = 'B'
Run Code Online (Sandbox Code Playgroud)


Rem*_*anu 11

select distinct a.id 
from table a
join table b on a.id=b.id
where a.type='A'
and b.type='B';
Run Code Online (Sandbox Code Playgroud)