mysql-编写SQL查询以删除Woocommerce中的旧订单

我想删除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数据库清洁的人.

解决方法:

这是通常的想法,首先删除子项,然后最后删除父项.未经测试请勿运行此程序.对于数据丢失,我不承担任何责任.

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'
上一篇:php-在管理新订单电子邮件模板中添加已应用的优惠券代码-WooCommerce


下一篇:php-在Woocommerce的存档页面上获取特定产品属性的子弹列表