在具有两列以上的两个表之间连接

时间:2014-11-28 10:40:38

标签: sql-server-2008

需要在4列上加入两个表

表A column1 column2 column3 column4 AAA
BBB
CCC
ABC
ACB
aaa zzz
CCD
DDD
BBC
bbb yyy
ccc zzz
fff xxx
FFF
fff zzz
fff rrr fff rrr ttt

表B
column1 column2 column3 column4 AAA
BBB
GH
ABC
GFD
aaa zzz
GFG
DDD
fvgg
bbb ggg
gff zzz
g xxx
FFF
ccc ty
fff rrr fff bb ttt

表'A'中的所有行都需要由4列的组合唯一标识 如果列为null,则无需加入该列。

2 个答案:

答案 0 :(得分:0)

为什么不简单:

SELECT a.column1 AS a_column1,
       a.column2 AS a_column2,
       a.column3 AS a_column3,
       a.column4 AS a_column4,
       b.column1 AS b_column1,
       b.column2 AS b_column2,
       b.column3 AS b_column3,
       b.column4 AS b_column4
FROM dbo.TableA a INNER JOIN dbo.TableB b
    ON a.column1 = b.column1 
   AND a.column2 = b.column2 
   AND a.column3 = b.column3 
   AND a.column4 = b.column4  

答案 1 :(得分:0)

select * from Table1 a INNER JOIN Table2 b 
on a.column1 = b.column1 AND 
a.column2 = b.column2 AND 
a.column3 = b.column3 AND 
a.column4 = b.column4