我想删除Woocommerce中的一些旧订单数据,仅保留最近12个月的订单数据
这是我用来删除所有订单的SQL查询:
DELETE FROM wp_woocommerce_order_itemmeta
DELETE FROM wp_woocommerce_order_items
DELETE FROM wp_comments WHERE comment_type = 'order_note'
DELETE FROM wp_postmeta WHERE post_id IN ( SELECT ID FROM wp_posts WHERE post_type = 'shop_order' )
DELETE FROM wp_posts WHERE post_type = 'shop_order'
2017年1月1日之前我需要添加什么来删除所有订单数据以及2017年1月1日起的KEEP订单数据?
我对SQL知之甚少,因为多年来我使用了一些SQL查询。
感谢任何帮助。
我很惊讶,目前还没有一个插件可以删除没有订单的客户。我怀疑我是唯一一个试图让他们的WP / WC数据库保持清洁的人。
答案 0 :(得分:3)
这是一般的想法,您先删除子项,然后删除父项。 不要在没有先测试的情况下运行。我对丢失的数据不承担任何责任。
DELETE
FROM wp_woocommerce_order_itemmeta
WHERE order_item_id IN (
SELECT order_item_id
FROM wp_woocommerce_order_items
WHERE order_id IN (
SELECT ID
FROM wp_posts
WHERE post_date < '2017-01-01'
)
)
DELETE
FROM wp_woocommerce_order_items
WHERE order_id IN (
SELECT ID
FROM wp_posts
WHERE post_date <= '2017-01-01'
)
DELETE
FROM wp_comments
WHERE comment_type = 'order_note'
AND comment_post_ID IN (
SELECT ID
FROM wp_posts
WHERE post_date <= '2017-01-01'
)
DELETE
FROM wp_postmeta
WHERE post_id IN (
SELECT ID
FROM wp_posts
WHERE post_type = 'shop_order'
AND post_date <= '2017-01-01'
)
DELETE
FROM wp_posts
WHERE post_type = 'shop_order'
AND post_date <= '2017-01-01'
答案 1 :(得分:0)
如果在这些表中实现了外键,那么只有当您能够从父表中删除数据时,才必须先从子表中删除数据。
SQL查询如下所示:
DELETE FROM wp_woocommerce_order_itemmeta
where Date(OrderDate Column) < '2017-01-01';
DELETE FROM wp_posts
WHERE post_type = 'shop_order'
and Date(OrderDate Column) < '2017-01-01';