日期:2014-05-18 浏览次数:20624 次
--> 测试数据: @表1
declare @表1 table (字段1 int,字段2 varchar(1))
insert into @表1
select 1,'a' union all
select 2,'a' union all
select 3,'a' union all
select 1,'b' union all
select 2,'b' union all
select 3,'b'
--> 测试数据: @表2
declare @表2 table (字段 int)
insert into @表2
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5
select a.*,b.字段2 from @表2 a left join @表1 b on a.字段=b.字段1 and b.字段2='a'
/*
字段 字段2
----------- ----
1 a
2 a
3 a
4 NULL
5 NULL
*/