2013-05-10 70 views
-1

我想查询我的opencart数据库,并且想要获取与某个产品相关的所有属性(如果可能的话)。SQL帮助 - 每个产品需要一行,每个属性不需要一行?

这是我到目前为止。它为每个产品提供所有属性,但是为每个属性创建一个全新的行。如果可能,我需要一行中的所有属性。

谢谢!

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

你想要做的是聚集在一起。你应该学习正确的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,大多数其他数据库具有用聚合串联字符串值类似的功能。在您的查询

+0

这给了我,我是用代码得到同样的结果我已经发布。 – 2013-05-11 04:57:14

+0

@BobbyLipps。 。 。在这种情况下,'text'可能来自属性表。鉴于我使用MySQL语法,我只是从'group by'子句中删除它。在其他数据库中,您需要使用'min()'或'max()'。 – 2013-05-11 15:49:02

+0

你好,我删除了该组,并且它帮助了一些我想。它把所有的属性名称放在一个表格单元格中,我不能说,但我认为它对文本列做了同样的事情。最后,它只返回一列。 – 2013-05-13 12:47:12

0

的样子,我知道你可能从未听说过的SQL查询JOIN ...

因此,这里是你的查询中使用JOIN小号改写:

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小号,这个查询会更快(取决于总行数)。

你的问题:可以使用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然后,您可以只回声“$结果[”属性] or爆炸it to get array of the attributes $ attributes = explode(',',$ result ['attributes']);`。总之,我想你是聪明的,你是刚装的属性名称,而不是实际的属性值对的事实...


编辑:

可以导出为CSV只一个普通的结构(每行一个入口) - 正是你要求的。平原结构意味着像product + product_descriptioncategory + 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 
+0

其实我想要属性值。简而言之,我想获取属性组名称并将这些列标题添加到列下的值中。上面的代码几乎给了我原来的代码。 – 2013-05-13 12:16:48

+0

然后,所需的结果不能通过**一个单独的SQL查询来实现** ...您必须单独执行 - 在一个查询中加载产品数据,然后为具体产品加载另一个查询中的属性。 “文本”列来自哪里?它包含什么值? – shadyyx 2013-05-13 12:26:48

+0

“文本”来自product_attribute表。我包含的产品属性(明显)从HTML代码链接到PDF的高度重量颜色等。 – 2013-05-13 12:43:22