我在网上商店有3张桌子:
jos_vm_category
category_id | distributor_code | ...
jos_vm_product
product_id | product_sku | ...
jos_vm_product_category_xref
category_id | product_id
我将新数据上传到临时表:
temp_product_traversal
product_sku | category_distributor_code | ...
我必须刷新jos_vm_product_category_xref表。
jos_vm_category.distributor_code与temp_product_traversal.category_distributor_code相同,两个表中的长度均为3,6,9个字符。 3个字符长代码是主要类别,6,9长代码是子类别的代码。我必须将产品放入其子类别和每个超级类别中。例如: 如果代码是VGAATIPCE,那么我必须将产品插入3个类别:
VGAATIPCE, VGAATI, VGA
因此,在这种情况下,我们需要在jos_vm_product_category_xref中添加3个新行。
是否可以使用一个mysql请求将3个新行插入到jos_vm_product_category_xref中?
答案 0 :(得分:1)
JOIN表达式可以在很多方面模仿WHERE表达式。你JOIN中的一些OR,你应该能够处理你正在寻找的东西。
INSERT jos_vm_product_category_xref(category_id, product_id)
SELECT c.category_id, p.product_id from temp_product_traversal trav
JOIN jos_vm_product p ON trav.product_sku = p.product_sku
JOIN jos_vm_category c ON LEFT(trav.category_distributor_code,3) = c.distributor_code
OR LEFT(trav.category_distributor_code,6) = c.distributor_code
OR trav.category_distributor_code = c.distributor_code
答案 1 :(得分:0)
我有另一个解决方案:
INSERT jos_vm_product_category_xref(product_id,category_id)
SELECT p.product_id,c.category_id
FROM
(
(
SELECT product_sku,LEFT(category_distributor_code,9) AS distributor_code
FROM temp_product_traversal
)
UNION
(
SELECT product_sku,LEFT(category_distributor_code,6) AS distributor_code
FROM temp_product_traversal
)
UNION
(
SELECT product_sku,LEFT(category_distributor_code,3) AS distributor_code
FROM temp_product_traversal
)
) AS trav,
jos_vm_product AS p,
jos_vm_category AS c
WHERE
p.product_sku=trav.product_sku
AND
c.distributor_code=trav.category_distributor_code