請檢查出來:
SELECT s.name, p.name, si.size, c.colour
FROM shops s
INNER JOIN product sp ON (sp.shop_id = s.id)
INNER JOIN products p ON (p.prod_id = sp.prod_id)
INNER JOIN sizes si ON (si.id = p.size)
INNER JOIN colours c ON (c.id = products.colour)
例子2:
SELECT s.name, p.name,
GROUP_CONCAT(si.size) AS sizes,
GROUP_CONCAT(c.colour) AS colours
FROM shops s
INNER JOIN product sp ON (sp.shop_id = s.id)
INNER JOIN products p ON (p.prod_id = sp.prod_id)
LEFT JOIN sizes si ON (si.id = p.size)
LEFT JOIN colours c ON (c.id = products.colour)
GROUP BY s.id, p.id
重要考慮:
- 你應該在你的表產品size_id,而是colour_id採用尺寸和顏色。
- 不確定是否所有產品都有尺寸和顏色,如果不是,請考慮將這兩者的內部連接切換爲LEFT JOIN;
- 您應該更改shop_product的產品表名稱;
- 表格的名稱應該是單數而非複數(不是義務);
- 您也可以通過店鋪和產品,並有串聯的顏色和大小的列(例如2)
謝謝你,麥地那,正是我一直在尋找的組。你的第一個解決方案奏效:) – Juned 2013-05-08 10:55:46