简体   繁体   English

SQL对多个列联接两个表

[英]SQL Joining two tables against multiple columns

Is there a way to join two tables against multiple columns for example in the first table we have:- 有没有一种方法可以将两个表连接到多个列,例如在我们拥有的第一个表中:

Headers = Name, AB1, AB2, AB3, AB4
Data = Lee, A, B, C ,D

Second table we have:- 我们有第二张桌子:

Headers = Type, Time
Data = A,1
Data = B,2
Data = C,3
Data = D,4

I'm looking to join both tables so I get the following so each of the AB columns would look up the Time Value from the other table. 我希望将两个表都连接起来,所以我得到了以下内容,因此每个AB列都将从另一个表中查找时间值。

Name, AB1, AB2, AB3, AB4, AB1_time, AB2_time, AB3_time, AB4_time,
Lee, A, B, C, D, 1, 2, 3, 4

I was looking to do multiple joins but don't really know how to best go about it. 我本来打算进行多个联接,但真的不知道如何最好地进行联接。 The data above is an simple example but in reality I have Two massive SQL Tables that I will then clash with other data sets. 上面的数据只是一个简单的示例,但实际上我有两个庞大的SQL表,然后将它们与其他数据集进行冲突。

You use multiple joins like this: 您可以使用多个联接,如下所示:

select t1.*, t2a.time. t2b.time, t2c.time, t2d.time
from t1 left join
     t2 t2a
     on t1.ab1 = t2a.type left join
     t2 t2b
     on t1.ab2 = t2b.type left join
     t2 t2c
     on t1.ab3 = t2c.type left join
     t2 t2d
     on t1.ab4 = t2d.type ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM