从小组联系中获得双倍价值

时间:2013-10-28 03:10:25

标签: sql

我遇到的问题是我从我认为应该获得的重复值中获得重复值。

这是我的sql:

    SELECT DISTINCT p.products_image,
                pd.products_name,
                p.products_id,
                p.products_model,
                p.manufacturers_id,
                m.manufacturers_name,
                p.products_price,
                p.products_sort_order,
                p.products_tax_class_id,
                pd.products_viewed,
                group_concat(p2i.icons_id separator ",") AS icon_ids,
                group_concat(pi.icon_class separator ",") AS icon_class,
                IF(s.status, s.specials_new_products_price, NULL) AS specials_new_products_price,
                IF(s.status, s.specials_new_products_price, p.products_price) AS final_price
FROM products p
LEFT JOIN specials s ON p.products_id = s.products_id
LEFT JOIN manufacturers m ON p.manufacturers_id = m.manufacturers_id
JOIN products_description pd ON p.products_id = pd.products_id
JOIN products_to_categories p2c ON p.products_id = p2c.products_id
INNER JOIN products_specifications ps7 ON p.products_id = ps7.products_id
LEFT JOIN products_to_icon p2i ON p.products_id = p2i.products_id
LEFT JOIN products_icons pi ON p2i.icons_id = pi.icons_id
WHERE p.products_status = '1'
  AND pd.language_id = '1'
  AND ps7.specification IN ('Polycotton' ,
                            'Reflective')
  AND ps7.specifications_id = '7'
  AND ps7.language_id = '1'
  AND p2c.categories_id = '21'
GROUP BY p.products_id
ORDER BY p.products_sort_order

获取双精度值的列是来自组concact的icon_ids。这种接缝只有在ploycotton和反射都是IN ps7.specification时才会发生。

如果它只是一个或那个,那么它可以正常工作。

products_to_icon表包含2列,products_id和icons_id。

如果一个产品有2个图标,则有2行,所以我很确定这是导致重复图标ID的事实。

当我运行时,带有2个图标的产品的icon_ids列是“4,4,6,6”,例如,当我需要的是“4,6”时

1 个答案:

答案 0 :(得分:0)

可能是一些愚蠢的东西,但我可以从您的查询中读取。我尝试了一些虚拟数据,

GROUP_CONCAT(`icon` )   product
icon1,icon1             Test1
icon2                   Test2
icon3                   Test3


SELECT GROUP_CONCAT(  `icon` ) , product
FROM  `Test1` 
WHERE 1 
GROUP BY  `product` 
LIMIT 0 , 30

这工作正常,我正在查询一个看起来像这样的表:

product icon
Test1   icon1
Test1   icon1
Test2   icon2
Test3   icon3

如果我添加另一行Test1 icon2

,这就是结果
GROUP_CONCAT(`icon` )   product
icon1,icon1,icon2   Test1
icon2   Test2
icon3   Test3

我忘了这一点,:)基本上我认为你的查询想法是正确的,但由于某种原因你有一个重复(可能是一些重复行的连接或分组错误)。尝试运行查询而不进行聚合,看看实际上是否有比预期更多的行。