我想查詢我的opencart數據庫,並且想要獲取與某個產品相關的所有屬性(如果可能的話)。SQL幫助 - 每個產品需要一行,每個屬性不需要一行?
這是我到目前爲止。它爲每個產品提供所有屬性,但是爲每個屬性創建一個全新的行。如果可能,我需要一行中的所有屬性。
謝謝!
SELECT
product.product_id,
product.model,
product_description.name,
attribute_description.name,
text
FROM
product,
product_attribute,
attribute,
attribute_description,
product_description
WHERE
product.product_id = product_attribute.product_id AND
product_attribute.attribute_id = attribute.attribute_id AND
attribute_description.attribute_id = attribute.attribute_id AND
product.product_id = product_description.product_id
order by
product_id, attribute_description.name;
下面是更新後的代碼:
SELECT
p.product_id,
p.model,
pd.name,
GROUP_CONCAT(ad.name ORDER BY ad.name SEPARATOR ',') AS attributes_group,
GROUP_CONCAT(pa.text ORDER BY ad.name SEPARATOR ',') AS attributes
FROM product p
LEFT JOIN product_attribute pa ON p.product_id = pa.product_id
LEFT JOIN attribute_description ad ON pa.attribute_id = ad.attribute_id
LEFT JOIN product_description pd ON p.product_id = pd.product_id
GROUP BY
p.product_id, p.model, pd.name
ORDER BY
p.product_id;
這給了我,我是用代碼得到同樣的結果我已經發布。 – 2013-05-11 04:57:14
@BobbyLipps。 。 。在這種情況下,'text'可能來自屬性表。鑑於我使用MySQL語法,我只是從'group by'子句中刪除它。在其他數據庫中,您需要使用'min()'或'max()'。 – 2013-05-11 15:49:02
你好,我刪除了該組,並且它幫助了一些我想。它把所有的屬性名稱放在一個表格單元格中,我不能說,但我認爲它對文本列做了同樣的事情。最後,它只返回一列。 – 2013-05-13 12:47:12