我遇到了一个问题,我从我认为我应该得到的东西中获得了重复的值。
这是我的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”时