1

给定以下数据库结构:

产品:

- id
- model
- manufacturerID
- subcontractorID

制造商ID 和分包商ID 是指同一张表,因为有些制造商也是分包商:

制造商:

- 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

...但我只能得到两个列值之一。

4

2 回答 2

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
于 2012-04-23T11:47:59.660 回答
0

试试这个:

LEFT JOIN Manufacturers AS mman ON p.manufacturerID = mman.id
LEFT JOIN Manufacturers AS msub ON p.subcontractorID = msub.id
于 2012-04-23T11:49:45.373 回答