-1

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

2 回答 2

0

查看您的查询,我意识到您可能从未听说过JOINSQL 查询...

JOIN所以这是您使用s重写的查询:

SELECT
    p.product_id,
    p.model,
    pd.name,
    ad.name,
    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
PRDER BY
    p.product_id, ad.name

现在,使用JOINs,这个查询会更快(取决于总行数)。

对于您的问题:GROUP_CONCAT()可以使用函数将所有属性加载为一行:

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

然后PHP你可以回显'$result['attributes'] orexplode it to get array of the attributes$attributes = explode(',', $result['attributes']);`。无论如何,我想你是明智的,你加载的只是属性名称,而不是实际的属性值对......


编辑:

您只能将普通结构(每行一个条目)导出到 CSV - 正是您所要求的。普通结构意味着像product+product_descriptioncategory+ category_description。如果您添加attribute++attribute_valueattribute_description,则结构不再简单。在这种情况下,导出到 XML 会好得多。

然后结构可能如下所示:

<?xml ... ?>
<products>
    <product>
        <title></title>
        <description></description>
        ...
        <attributes>
            <attribute>
                <title></title>
                <description></description>
                <value></value>
            </attribute>
            <attribute>
                <title></title>
                <description></description>
                <value></value>
            </attribute>
            ...
        </attributes>
    </product>
    ...
</products>

但是也许您仍然可以使用一个 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
于 2013-05-12T13:04:07.330 回答
0

您要做的是将它们聚合在一起。您应该学习正确的join语法,但我不打算在这里解决这个问题(将连接条件放在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,大多数其他数据库都具有类似的功能,用于在聚合中连接字符串值。

于 2013-05-10T20:37:17.323 回答