我正在尝试查询我的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;
答案 0 :(得分: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,大多数其他数据库都具有类似的功能,用于在聚合中连接字符串值。
答案 1 :(得分:0)
查看您的查询我意识到您可能从未在SQL查询中听说过JOIN
...
所以这是您使用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
现在,使用JOIN
s,此查询会更快(取决于行总数)。
致你的问题:可以使用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'] or
explode it to get array of the attributes
$ attributes = explode(',',$ result ['attributes'] );`。无论如何,我猜你明智的是,你只加载属性名称,而不是实际的属性 - 值对......
修改强>
您只能将CSV导出为普通结构(每行一个条目) - 正是您要求的内容。简单结构意味着product
+ product_description
或category
+ category_description
。如果您添加attribute
+ attribute_value
+ attribute_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