鉴于是遵循数据库结构:
产品:
- id
- model
- manufacturerID
- subcontractorID
manufacturerID和subcontractorID引用同一个表,因为一些制造商也是分包商:
制造商:
- id
- name
现在,如果我想选择这两个列,我该怎么做?
我尝试过这样的事情......
SELECT p.model, m.name AS Manufacturer, m.name AS Subcontractor
FROM Product AS p
LEFT JOIN Manufacturers AS m ON p.manufacturerID = m.id
LEFT JOIN m ON p.subcontractorID = m.id
...但我只能获得两个列值之一。
答案 0 :(得分:1)
关闭 - 您需要两次加入制造商表:
SELECT
p.model, manufacturer.name AS Manufacturer,
subcontractor.name AS Subcontractor
FROM Product AS p LEFT JOIN
Manufacturers AS manufacturer
ON p.manufacturerID = manufacturer.id LEFT JOIN
Manufacturers AS subcontractor
ON p.subcontractorID = subcontractor.id
答案 1 :(得分:0)
试试这个:
LEFT JOIN Manufacturers AS mman ON p.manufacturerID = mman.id
LEFT JOIN Manufacturers AS msub ON p.subcontractorID = msub.id