在SQL中,有时要实现多列对应一列的查询,可以用left join来多连:
表1
tabName
id | name |
1 | 一 |
2 | 二 |
3 | 三 |
表2
table1
id | name1 | name2 | name3 |
1 | 3 | 2 | 1 |
2 | 1 | 1 | 2 |
其中表2的name1,name2name3对应表1的id,现在要把表2的name1.2.3的id换成名字name,方法如下:
SELECT t1.id,n1.name,n2.name,n3.name from table1 as t1 left join tabName as n1 on t1.name1=n1.id left join tabName as n2 on t1.name2=n2.id left join tabName as n3 on t1.name3=n3.id