我想查询我的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