-1

opencart データベースにクエリを実行しようとしていますが、その特定の製品に関連するすべての属性を連続して取得したいと考えています (可能な場合)。

これが私がこれまでに持っているものです。各製品のすべての属性を提供しますが、属性ごとにまったく新しい行を作成します。可能であれば、すべての属性を 1 行にまとめたいと思います。

ありがとう!

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;
4

2 に答える 2