我有一个基于OpenCart 3的多语言网站,我已经在下面包含了必要的表格和SQLFiddle。
我需要为每个产品分隔逗号分隔<Provider>
,如下所示:
category name
其中product_id, language_id, product_name
是以逗号分隔的类别列表。
产品名称示例:product_name
实际上是类别:Nightgown, Wrap robes, 2li
重要说明:最后,我想在product_description表的8, 188, 192
列中填入我上面解释的逗号分隔的类别列表。
我似乎应该使用name
,但它远远超出了我的SQL知识,即使我花了超过半天的时间。
这是SQLFiddle:http://sqlfiddle.com/#!9/0337c3
模式
GROUP_CONCAT
如果你告诉我这是什么问题我会很感激吗?
答案 0 :(得分:1)
您可以使用以下select语句:
select
pd.product_id,
pd.language_id,
group_concat(cd.name order by cd.language_id separator ', ') as product_name
from product_description pd
left join product_to_category pc using (product_id)
left join category_description cd using (category_id, language_id)
group by pd.product_id, pd.language_id
我认为将冗余数据存储在新列中并不是一个好主意。但你可以用:
update product_description pd
join (
select
pd.product_id,
pd.language_id,
group_concat(cd.name order by cd.language_id separator ', ') as product_name
from product_description pd
left join product_to_category pc using (product_id)
left join category_description cd using (category_id, language_id)
group by pd.product_id, pd.language_id
) sub using (product_id, language_id)
set pd.name = sub.product_name;
答案 1 :(得分:1)
以下查询应该有效:
select pd.product_id, pd.language_id,group_concat(distinct cd.name separator ',') as Product_name
from product_description pd
inner join product_to_category pc
on pc.product_id = pd.product_id
inner join category_description cd
on pc.category_id = cd.category_id and pd.language_id = cd.language_id
group by pd.product_id, pd.language_id;
更新表格:
Update product_description p
set p.name = (select group_concat(distinct cd.name separator ',') as Product_name
from (select *from product_description) pd
inner join product_to_category pc
on pc.product_id = pd.product_id
inner join category_description cd
on pc.category_id = cd.category_id and pd.language_id = cd.language_id
group by pd.product_id, pd.language_id
having p.product_id = pd.product_id and p.language_id = pd.language_id);
希望它有所帮助!