主表希望两次使用同一列的reffrence表连接两次。 主表列是带整数的insert,需要与refference表连接以使字符返回以便为用户显示。 下面的sql没有显示任何记录,为什么?
主要表BD_BRAND
Sports_BR Leather_BR
2 1
参考表BD_REF
ID NME REF_TYPE
1 NIKE Sports_Brand
2 ADIDAS Sports_Brand
3 PUMA Sports_Brand
1 CLACKS Leather_Brand
2 LOTTUSSE Leather_Brand
3 CHEANEY Leather_Brand
SELECT B.NME AS Sports_BR, C.NME AS Leather_BR
FROM BD_BRAND A
LEFT JOIN BD_REF B on B.ID = A.Sports_BR
LEFT JOIN BD_REF C on C.ID = A.Leather_BR
我想要的结果如下:
Sports_BR Leather_BR
ADIDAS CLACKS
答案 0 :(得分:0)
我认为你在寻找:
SELECT B.NME AS Sports_BR, C.NME AS Leather_BR
FROM BD_BRAND A
LEFT JOIN BD_REF B on B.ID = A.Sports_BR and B.REF_TYPE = 'Sports_Brand'
LEFT JOIN BD_REF C on C.ID = A.Leather_BR and B.REF_TYPE = 'Leather_Brand'
由于参考类型不同,您的ID会多次出现,因此您需要指定参考类型要抓取的ID。