11,850
社区成员




if object_id('A') is not null
drop table A
create table A(id int, type varchar(10))
insert into A
select 1 ,'a' union all
select 1 ,'b' union all
select 1 ,'c' union all
select 1 ,'d' union all
select 2 ,'a' union all
select 3 ,'g' union all
select 4 ,'b'
select id from A where type='a'
intersect
select id from A where type='b'
select id
from tb t
where [type] = 'a' and exists (select 1 from tb where id = t.id and [type] = 'b')
use tempdb;
/*
create table test
(
id int not null,
[type] varchar(5) not null
);
insert into test(id,[type])
values
(1,'a'),
(1,'b'),
(1,'c'),
(1,'d'),
(2,'a'),
(3,'g'),
(4,'b');
*/
select t1.id
from test as t1
join test as t2
on t1.id = t2.id
where t1.type = 'a'
and t2.type = 'b';