日期:2014-05-18 浏览次数:20572 次
--> 测试数据:[t1] if object_id('[t1]') is not null drop table [t1] create table [t1]([id] int,[code] varchar(2)) insert [t1] select 1,'a1' union all select 2,'a2' union all select 3,'a3' --> 测试数据:[t2] if object_id('[t2]') is not null drop table [t2] create table [t2]([id] int,[id_t1] int,[f1] varchar(2)) insert [t2] select 1,1,'t1' union all select 2,1,'t2' union all select 3,3,'t6' select distinct t2.id_t1,t1.code from t2 inner join t1 on t2.id_t1=t1.id id_t1 code 1 a1 3 a3
------解决方案--------------------
select m.* from #t1 m where exists(select 1 from #t2 n where m.id = n.id_t1)
------解决方案--------------------
create table [#t1]([id] int,[code] varchar(2)) insert [#t1] select 1,'a1' union all select 2,'a2' union all select 3,'a3' create table [#t2]([id] int,[id_t1] int,[f1] varchar(2)) insert [#t2] select 1,1,'t1' union all select 2,1,'t2' union all select 3,3,'t6' select m.* from #t1 m where exists(select 1 from #t2 n where m.id = n.id_t1) drop table #t1 , #t2 /* id code ----------- ---- 1 a1 3 a3 (所影响的行数为 2 行) */