我有一个WooCommerce商店,我希望运行一个MySQL查询来删除特定公司的所有wp_posts(订单),该公司包含在wp_postmeta表中的meta_key / meta_value对中。
到目前为止我做了什么
我已经阅读了这篇文章:Mysql delete all posts that have a given meta_key这篇文章非常实用,我编写了这段代码:
delete a,b,c,d,e,f,g,h,i /* This bit tells us what to delete*/
FROM wp2_posts a
LEFT JOIN wp2_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp2_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp2_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp2_terms e ON ( e.term_id = d.term_id )
LEFT JOIN wp2_woocommerce_order_items f ON ( a.ID = f.order_id )
LEFT JOIN wp2_woocommerce_order_itemmeta g ON ( f.order_item_id = g.order_item_id )
LEFT JOIN wp2_comments h ON ( a.ID = h.comment_post_ID )
LEFT JOIN wp2_commentmeta i ON ( h.comment_ID = i.comment_id )
WHERE a.ID IN /* This provides the args for deletion*/
( SELECT post_id
FROM wp2_postmeta
WHERE meta_key LIKE '_shipping_company'
AND meta_value LIKE 'Acme Corp'
)
我在哪里挣扎
但是我在尝试在SQL pro中运行查询时得到的响应是“你不能在FROM子句中为更新指定目标表'c'。”
我认为它说我不能删除我用作参数的东西。
非常感谢任何想法,劳拉。
答案 0 :(得分:1)
MySQL不允许您同时修改和选择同一个表。在这种情况下,您对DELETE
表格运行SELECT
和wp2_postmeta
。
有很多方法可以解决这个问题(例如参见MySQL Error 1093 - Can't specify target table for update in FROM clause),但在这种情况下,您不需要WHERE
子句中的子查询,因为您可以查看{{直接在meta_key
表的1}}和meta_value
列。删除子查询中的wp2_postmeta
语句可以解决问题:
SELECT
原始查询的更改:
DELETE c,a,b,d,e,f,g,h,i
FROM wp2_postmeta c
JOIN wp2_posts a ON ( a.ID = c.post_id )
LEFT JOIN wp2_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp2_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp2_terms e ON ( e.term_id = d.term_id )
LEFT JOIN wp2_woocommerce_order_items f ON ( a.ID = f.order_id )
LEFT JOIN wp2_woocommerce_order_itemmeta g ON ( f.order_item_id = g.order_item_id )
LEFT JOIN wp2_comments h ON ( a.ID = h.comment_post_ID )
LEFT JOIN wp2_commentmeta i ON ( h.comment_ID = i.comment_id )
WHERE c.meta_key LIKE '_shipping_company'
AND c.meta_value LIKE 'Acme Corp'
开始wp2_postmeta
秒wp2_posts
子句meta_key
和meta_value
进行过滤