0

我遇到了一个问题,我从我认为我应该得到的东西中获得了重复的值。

这是我的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 和反射都在 ps7.specification 中时才会发生这种接缝。

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

products_to_icon 表包含 2 列 products_id 和 icons_id。

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

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

4

1 回答 1

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

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

于 2013-10-28T03:51:52.593 回答