简体   繁体   中英

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.

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.

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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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