我有以下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_tax_class_id,
pd.products_viewed,
group_concat(p2i.icons_id separator ",") as icons_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 ('Cotton' , 'Fireproof' )
AND ps7.specifications_id = '7'
AND ps7.language_id = '1'
and p2c.categories_id = '21'
group by p.products_id order by pd.products_name
当 1 种产品仅在 1 种规格中时,这非常有效。但是,当产品有 2 种规格(如棉质和防火)时,product_icons 会翻倍。
我选择了 distinct,但因为它在 2 个规范中,它正在检索 2x 图标,我不知道为什么(在我看来, distinct 应该在选择图标 ID 两次之前处理好这个问题。)
我错过了什么?
更新澄清:
我从上述查询中得到的结果显示在这里:http: //i.imgur.com/aD6JM0q.png
请注意,第 3 行的列 icon_id 两次显示相同的 icon_id 5,1,5,1
我的目标是只显示一次这些 ID,所以它应该是 5,1
我选择的规格越多,由于属于几种不同规格的产品,重复的机会就越大。
为了进一步澄清,这里是 products_to_icons 和 products_icons 表的一些示例图像
Products_to_icons: http://i.imgur.com/gBhlCri.png products_icons: http://i.imgur.com/1134Rb1.png