SQL帮助-每个产品需要一行,而不是每个属性需要一行?

SQL帮助-每个产品需要一行,而不是每个属性需要一行?,sql,opencart,Sql,Opencart,我正在尝试查询我的opencart数据库,如果可能的话,我想获取一行中与特定产品相关的所有属性 这是我到目前为止所拥有的。它为每个产品提供所有属性,但为每个属性创建一个全新的行。如果可能,我希望所有属性都在一行中 谢谢 SELECT product.product_id, product.model, product_description.name, attribute_description.name, text FROM product,

我正在尝试查询我的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;

您要做的是将它们聚合在一起。您应该学习正确的连接语法,但我不打算在这里将连接条件放在on子句而不是where子句中

下面是MySQL语法,可以实现您想要的功能:

SELECT
    product.product_id,
    product.model,
    product_description.name,
    group_concat(attribute_description.name) as attributes,
    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
group by 
    product.product_id,
    product.model,
    product_description.name;

如果您不使用MySQL,大多数其他数据库都有类似的函数,用于在聚合中连接字符串值。

查看您的查询,我意识到您可能从未听说过SQL查询中的JOIN

下面是使用联接重写的查询:

在PHP中,您可以只回显“$result['attributes']或explodeit以获取属性数组$attributes=explode',”,$result['attributes'];`。无论如何,我想你是明智的事实,你只是加载属性名,而不是实际的属性值对

编辑:

您只能将每行一个条目导出到CSV,这正是您所请求的。简单结构表示类似于产品+产品描述,或类别+类别描述。如果添加属性+属性\值+属性\描述表,则结构不再简单。在这种情况下,导出到XML会更好

然后,结构可以如下所示:

... ... ... 但是仍然可以使用一个SQL并导出到CSV,但是在一列中将属性名逗号连接,在另一列中将属性值逗号连接。。。无法用于自动导入或由phpMyAdmin导入。。。无论如何,您可以尝试以下方法:

SELECT
    p.product_id,
    p.model,
    pd.name,
    GROUP_CONCAT(ad.name ORDER BY ad.name SEPARATOR ',') AS attributes,
    GROUP_CONCAT(pa.text ORDER BY ad.name SEPARATOR ',') AS values /* <- problem could be with values ordering */
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

这给了我与我发布的代码完全相同的结果。@BobbyLipps。在这种情况下,文本可能来自attributes表。考虑到我使用的是MySQL语法,我只是将其从GROUPBY子句中删除。在其他数据库中,您需要使用min或max。您好,我通过删除了组,我想这有点帮助。它将所有属性名放在一个表单元格中,我不知道,但我认为它对文本列也做了同样的事情。最后,它只返回了一列。实际上我需要属性值。简言之,我希望获取属性组名称,并将其作为列标题,然后在列下添加值。上面的代码基本上给出了我的原始代码所做的一切。然后,一个SQL查询无法实现所需的结果……您必须单独执行—在一个查询中加载产品数据,然后在另一个查询中加载具体产品的属性。文本列从何处提交?它包含哪些值?文本来自产品属性表。我包含产品的属性,范围从链接到PDF的html代码到高度-重量-颜色等。然后文本列就是为什么GroupBy没有效果的原因。好的,也许我应该问一下,为什么你需要在一行一个条目中加载所有属性和它的值以及产品数据?也许我们可以为您的问题想出其他解决方案……简言之,我需要创建一个视图,在其中我可以导出到csv。以下是查询到目前为止正在做的事情,随着时间的推移,我也在上面发布了我的新代码。
SELECT
    p.product_id,
    p.model,
    pd.name AS product_name,
    GROUP_CONCAT(ad.name ORDER BY ad.name SEPARATOR ',') AS attributes,
    pd.text /* <- don't know where the column is coming from, probably something custom */
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, pd.text
ORDER BY
    p.product_id
SELECT
    p.product_id,
    p.model,
    pd.name,
    GROUP_CONCAT(ad.name ORDER BY ad.name SEPARATOR ',') AS attributes,
    GROUP_CONCAT(pa.text ORDER BY ad.name SEPARATOR ',') AS values /* <- problem could be with values ordering */
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