我正在将产品从一个系统迁移到Magento,并且在旧系统中存在组/子产品关系,我必须在Magento系统中维护
我目前的表结构:
[id], [sku], [name], [parent id]
202182 |240330 |parent product |
202183 |240331 |child product 1|[202182][Parent-Product-240330]
202184 |240332 |child product 2|[202182][Parent-Product-240330]
我想要实现的目标:
[id], [sku], [name], [product_type], [associated_sku]
202182 |240330 | parent product |grouped |240331=0.0000,240332=0.0000
202183 |240331 | child product 1|simple |
202184 |240332 | child product 2|simple |
你能帮我写一个查询来实现它吗?
如果层次结构只有两个级别,则完全可以在MySQL 5.6中执行此操作,因此您就像您一样出色(如果它具有可变数量的级别,则需要MySQL 8.x)。
我将使用的查询是:
select
p.id,
p.sku,
p.name,
'grouped' as product_type,
group_concat (concat(c.id, '=0.0000') order by c.id) as associated_sku
from product p
join product c on c.parent_id = p.id
group by p.id, p.sku, p.name
union all
select
id,
sku,
name,
'simple' as product_type,
null as associated_sku
from product
where parent_id is not null
注意:此查询假定父行上的 parent_id
值为 null。