我尝试了3种不同的删除方法。 如果条件合适,我想删除表buylist_products中的行。
尝试1:
DELETE FROM parkage_buylist_products bp, parkage_productsmeta pm, parkage_buylist b WHERE bp.product_id = pm.product_id AND (pm.meta_key = 'we_buy_price' OR pm.meta_key = 'we_buy_rule') AND pm.meta_value = 0 AND bp.order_id = b.order_id AND b.status = 'in_progress' AND b.order_id = 24
尝试2:
DELETE FROM parkage_buylist_products bp
LEFT JOIN parkage_productsmeta pm, parkage_buylist b
ON bp.product_id = pm.product_id
WHERE (pm.meta_key = 'we_buy_price' OR pm.meta_key = 'we_buy_rule')
AND pm.meta_value = 0
AND bp.order_id = b.order_id
AND b.status = 'in_progress'
AND b.order_id = 24
尝试3:
DELETE FROM parkage_buylist_products bp, parkage_buylist b
WHERE bp.order_id = b.order_id
AND b.status = 'in_progress'
AND b.order_id = 24
AND bp.product_id IN (SELECT pm.product_id
FROM parkage_productsmeta pm
WHERE (pm.meta_key = 'we_buy_price' OR pm.meta_key = 'we_buy_rule')
AND pm.meta_value = 0
对于我尝试过的第一个,删除bp,删除bp。*它会给我一个语法错误。
感谢您的帮助
答案 0 :(得分:0)
请尝试以下方法......
DELETE parkage_buylist_products,
parkage_productsmeta,
parkage_buylist
FROM parkage_buylist_products
JOIN parkage_productsmeta
JOIN parkage_buylist
WHERE parkage_buylist_products.product_id = parkage_productsmeta.product_id
AND parkage_buylist_products.order_id = parkage_buylist.order_id
AND ( parkage_productsmeta.meta_key = 'we_buy_price' OR
parkage_productsmeta.meta_key = 'we_buy_rule' )
AND parkage_productsmeta.meta_value = 0
AND parkage_buylist.status = 'in_progress'
AND parkage_buylist.order_id = 24;
使用条件delete from many tables at once sql
进行快速搜索后,我在delete from two tables in one query找到了该页面。我从他们最受欢迎的答案(由Eric)中选择了第二个选项并使用您的代码进行了修改。
根据我的研究,这应该从DELETE
那些符合指定条件的记录后面指定的表中删除,这些记录与您的问题中的相同。请注意,我已经删除了别名 - 我更喜欢在没有它们的情况下工作。大概你仍然可以在这里使用它们。
如果您有任何问题或意见,请随时发表评论。