表A
tt vv
a AA
b BB
c CC
1003 DD
1001 EE
1002 FF
d GG
f hh表B
ii aa
1003 中国
1002 美国
1001 地球要得到
表C
tt vv
a AA
b BB
c CC
中国 DD
地球 EE
美国 FF
d GG
e HH直接select B.ii,A.vv from A join B on A.tt=B.ii只能查询到表B中有对应值的记录,查不到A.tt为a,b,c...的记录
tt vv
a AA
b BB
c CC
1003 DD
1001 EE
1002 FF
d GG
f hh表B
ii aa
1003 中国
1002 美国
1001 地球要得到
表C
tt vv
a AA
b BB
c CC
中国 DD
地球 EE
美国 FF
d GG
e HH直接select B.ii,A.vv from A join B on A.tt=B.ii只能查询到表B中有对应值的记录,查不到A.tt为a,b,c...的记录
select isnull(B.ii,A.tt) tt,A.vv from A left join B on A.tt=B.ii
declare @表A table (tt varchar(4),vv varchar(2))
insert into @表A
select 'a','AA' union all
select 'b','BB' union all
select 'c','CC' union all
select '1003','DD' union all
select '1001','EE' union all
select '1002','FF' union all
select 'd','GG' union all
select 'f','hh'declare @表B table (ii varchar(4),aa varchar(4))
insert into @表B
select '1003','中国' union all
select '1002','美国' union all
select '1001','地球'select ISNULL(b.aa,a.tt) tt ,a.vv from @表A a LEFT JOIN
@表B b ON a.tt=b.ii/*
tt vv
---- ----
a AA
b BB
c CC
中国 DD
地球 EE
美国 FF
d GG
f hh
*/