我正在尝试查询我的 opencart 数据库,并且我想连续获取与该特定产品相关的所有属性(如果可能的话)。
这是我到目前为止所拥有的。它为每个产品提供所有属性,但为每个属性创建一个全新的行。如果可能,我希望将所有属性放在一行中。
谢谢!
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;