select B.ID,B.pairID,A.Type as pairType from 表2 B left join 表1 A On B.PairID=A.ID
--> 测试数据: [表1] if object_id('[表1]') is not null drop table [表1] create table [表1] (ID int,TYPE varchar(2)) insert into [表1] select 1,'AA' union all select 2,'BB' union all select 3,'CC' union all select 4,'DD' union all select 5,'EE' union all select 6,'FF' --> 测试数据: [表2] if object_id('[表2]') is not null drop table [表2] create table [表2] (ID int,PairID int) insert into [表2] select 1,2 union all select 2,1 union all select 3,4 union all select 4,5 union all select 5,null union all select 6,null goselect a.*,b.type from [表2] a left join [表1] b on a.pairid=b.id
select * from 表2 a left join 表1 b on a.pairid=b.id
from 表2 B
left join 表1 A On B.PairID=A.ID
if object_id('[表1]') is not null drop table [表1]
create table [表1] (ID int,TYPE varchar(2))
insert into [表1]
select 1,'AA' union all
select 2,'BB' union all
select 3,'CC' union all
select 4,'DD' union all
select 5,'EE' union all
select 6,'FF'
--> 测试数据: [表2]
if object_id('[表2]') is not null drop table [表2]
create table [表2] (ID int,PairID int)
insert into [表2]
select 1,2 union all
select 2,1 union all
select 3,4 union all
select 4,5 union all
select 5,null union all
select 6,null
goselect a.*,b.type from [表2] a left join [表1] b on a.pairid=b.id