我试图改变我的代码,所以当没有匹配的行时," ean13"在webshop_stock中它必须INSERT。
但是mysql不接受我测试的代码,我尝试了一些东西以使其工作。我在Google上的搜索没有找到一个有效的例子" INSERT INTO - JOIN - ON DUPLICATE KEY UPDATE"这可能吗?
此时的问题是我的行在不存在时会被创建,但存在的行不会更新。
测试了以下代码:
$get_input = "
INSERT INTO webshop_stock
(id_warehouse,id_product,id_product_attribute,ean13, physical_quantity, usable_quantity)
SELECT
'1',
pa.id_product,
pa.id_product_attribute,
pa.ean13,
ai.quantity,
ai.quantity
FROM
webshop_product_attribute pa,
Adcount_input ai
WHERE
pa.ean13 = ai.ean13
AND NOT EXISTS
(SELECT id_product_attribute FROM webshop_stock WHERE id_product_attribute = pa.id_product_attribute)
ON DUPLICATE KEY UPDATE
physical_quantity = ai.quantity,
usable_quantity = ai.quantity
";
原始代码:
$get_input = "
UPDATE
webshop_stock AS s
JOIN(
SELECT
pa.ean13,
pa.id_product_attribute,
pa.id_product,
ai.quantity
FROM
webshop_product_attribute pa,
Adcount_input ai
WHERE
pa.ean13=ai.ean13) q
SET
s.id_warehouse = 1,
s.id_product = q.id_product,
s.id_product_attribute = q.id_product_attribute,
s.ean13 = q.ean13,
s.physical_quantity = q.quantity,
s.usable_quantity = q.quantity
WHERE
s.id_product_attribute = q.id_product_attribute";
答案 0 :(得分:2)
已解决此代码可以满足我的需求。
$get_input = "
INSERT INTO webshop_stock
(id_warehouse,id_product,id_product_attribute,ean13, physical_quantity, usable_quantity)
SELECT
'1',
pa.id_product,
pa.id_product_attribute,
pa.ean13,
ai.quantity,
ai.quantity
FROM
webshop_product_attribute pa,
Adcount_input ai
WHERE
pa.ean13 = ai.ean13
ON DUPLICATE KEY UPDATE
physical_quantity = ai.quantity, usable_quantity = ai.quantity
";
答案 1 :(得分:1)
语法为:
INSERT <table> <field list>
SELECT ... JOIN ...
mysql中有 NO INSERT JOIN SELECT
。它是INSERT SELECT JOIN
。