我有2张相同信息的表格,包括:身份证,姓名,地址,邮政编码,电话号码。但是,每张桌子上的同一个人的身份证不同。如何创建一个十字路口表以根据其他信息匹配id?
EX)
表1
ID | Name | Address | Zip | Phone
-----------------------------------------------------
1111 | John Doe | 123 Fake St | 12345 | 123-456-7890
-----------------------------------------------------
表2
ID | Name | Address | Zip | Phone
-----------------------------------------------------
2222 | John Doe | 123 Fake St | 12345 | 123-456-7890
-----------------------------------------------------
Desired Crosswalk
Table1ID | Table2ID
---------------------
1111 | 2222
---------------------
非常感谢任何见解。 谢谢!
答案 0 :(得分:3)
您可以将匹配的列上的两个表连接起来:
SELECT Table1.ID AS Table1ID, Table2.ID AS Table2ID
FROM Table1
INNER JOIN Table2 ON Table1.Name = Table2.Name
AND Table1.Address = Table2.Address
AND Table1.Zip = Table2.Zip
AND Table1.Phone = Table2.Phone
在这里,我加入了所有列,您必须将其调整为实际匹配的列...
答案 1 :(得分:0)
正如in this article and comments解释的那样,您可以使用JOIN通过表别名匹配两个表中的所有列:
SELECT t1.id Tab1Id, t2.id Tab2Id
FROM t1
INNER JOIN t2 on tb1.name = t2.name
INNER JOIN t2 tb2_1 on t1.address = tb2_1.address
INNER JOIN t2 tb2_2 on t1.zip = tb2_2.zip
INNER JOIN t2 tb2_3 on t1.phone = tb2_3.phone