我有两个表tableCode和tableName,如下所示:
tableCode
code1, code2, code3, name3
1, 1, 1, code31name
tableName
type, typeName, code, name
c1, code1, 1, code11name
c2, code2, 1, code21name
我想选择结果如下
1,code11name,1,code21name,1,code31name
我只知道如下,你能帮助我吗?
select m1.code1, m2.name, m1.code2, m3.name, m1.code3, m1.name3
from tableCode m1
left join tableName m2 on m1.code1 = m2.code and m2.type ='c1'
left join tableName m3 on m1.code1 = m3.code and m3.type ='c2'
答案 0 :(得分:0)
您还需要一次加入。我认为您对表别名感到困惑:
select c.code1, n1.name, c.code2, n2.name, c.code3, n3.name3
from tableCode c left join
tableName n1
on n1.code = c.code1 and n1.category = 'c1' left join
tableName n2
on n2.code = c.code2 and n2.category = 'c2' left join
tableName n3
on n3.code = c.code3 and n3.category = 'c3';