目前我要加入三个表。我有从一个系统(旧)迁移到另一个系统(新)的数据。我需要比较此数据,以确保匹配但不匹配。我有三张桌子。一个有要移动的帐户列表。这两个系统具有不同的ID类型,因此该第一个表是两个表的所有ID的列表,以及每个移动的帐户。这就是我的基本人口。
ID1 ID2
ABC 123
ABC 123
ABC 123
DEF 456
DEF 456
DEF 456
然后我有了表2,它是旧系统中的所有数据。
ID Fname Lname
ABC John Smith
ABC Tom Smith
ABC Kate Smith
DEF Jason Thomas
DEF Ruby Thomas
DEF Alex Johnson
然后表3是在新系统中找到的所有数据。
ID Fname Lname
123 John Smith
123 Tom Smith
123 Kate Smith
456 Jason Thomas
456 Ruby Thomas
现在,当我在ID上连接这些表时,得到的行比我需要的多得多。
我加入时会收到以下消息:
ID Fname_old Lname_old ID2 Fname_new Lname_new
ABC John Smith 123 John Smith
ABC John Smith 123 Tom Smith
ABC John Smith 123 Kate Smith
我正在尝试将它们仅在返回匹配行的地方加入它们,如果找不到匹配项,我仍然应该从ID文件中获取ID,并从表2(旧数据)中获取数据,因为这是发送到新系统的数据。
ID1 ID2 Fname_old Lname_old Fname_new Lname_new
ABC 123 John Smith John Smith
ABC 123 Tom Smith Tom Smith
ABC 123 Kate Smith Kate Smith
DEF 456 Jason Thomas Jason Thomas
DEF 456 Ruby Thomas Ruby Thomas
DEF 456 Alex Johnson
我使用的代码是:
Select a.ID1,a.ID2,b.fname as fname_old,b.lnam as lname_old,c.fname as fname_new,c.lname as lname_new
from table1 a
left join table2 b
on a.ID1 = b.ID
left join table3 c
on a.ID2 = c.ID