将2个不同表中的每列合并为一个表

时间:2018-04-10 03:01:56

标签: sql

我想将这些表列合并到一个表中

表1

account_number currentAddress   
CCA2312311     Address1   
CCA2312311     Address2

表2

account_number proposedAddress
  CCA2312311     Address1
  CCA2312311     Address2
  CCA2312311     Address3

结果应该是

account_number  currentAddress proposedADdress
CCA2312311       Address1       Address1
CCA2312311       Address2       Address2
CCA2312311       Null           Address3

2 个答案:

答案 0 :(得分:0)

我认为一个简单的正确联接会做。 (注意右连接返回右边的所有表,因为我认为这是你想要的)。如果要返回同时具有currentAddress和ProposedAddress(使用内部连接)的account_number

select A. CurrentAddress, 
       B. ProposedAddress 
from [table 1] A --please note that for table names, best not to have space   
right join [table 2] B 
on A. account_number = B. account_number

请在此链接中找到帮助:http://www.sql-join.com/sql-join-types/

答案 1 :(得分:0)

Select t2.account_number,t2.proposedaddress, t1.currentaddress
From table2 t2 left outer join table1 t1 on    
(t1.account_number=t2.account_number and   
Upper(Trim(t1.currentaddress)) like Upper(Trim(t2.proposedaddress)))