phpmyadmin 如何从数据库中删除woocommerce属于特定类别的产品

c2e8gylq  于 2022-11-09  发布在  PHP
关注(0)|答案(2)|浏览(167)

我需要一个MySql查询语句来删除属于一个类别或不属于另一个类别的所有woocommerce产品,类似于:

DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_term_taxonomy.term_id NOT IN (29,31,32,33,34) AND post_type IN ('product','product_variation')
GROUP BY wp_posts.ID);

而且我正在处理大量数据,涉及100,000多种产品。

xpszyzbs

xpszyzbs1#

如果任何人都有同样的需要。这是我如何解决删除:
1-最重要的是要确保类别之间没有重叠,比如您要删除的产品没有附加其他类别。这可以手动完成(如果您的产品很少),也可以通过查询来取消附加不必要的关系。
2-接下来,我将建议删除每次只属于一个类别的产品,查询应该是具体的,以防止任何不希望的结果和冲突。
3-在删除之前选择产品,以查看是否与此查询有任何冲突:

SELECT * FROM wp_posts WHERE ID IN(SELECT posts.object_id from (SELECT object_id FROM wp_term_relationships
LEFT JOIN wp_posts ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE term_taxonomy_id = 38 AND post_type IN ('product','product_variation')
GROUP BY object_id) as posts);

4-每次只删除一个通缉类别(BY ID)商品:

DELETE FROM wp_posts WHERE ID IN(SELECT posts.object_id from (SELECT object_id FROM wp_term_relationships
LEFT JOIN wp_posts ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE term_taxonomy_id = 38 AND post_type IN ('product','product_variation')
GROUP BY object_id) as posts);

对每个类别的30,000多个产品执行上述查询花费了1秒。

bakd9h0s

bakd9h0s2#

接受的答案将删除产品,但保留 meta数据。

DELETE FROM wp_postmeta WHERE post_id IN( SELECT id FROM wp_posts WHERE ID IN(SELECT posts.object_id from (SELECT object_id FROM wp_term_relationships
LEFT JOIN wp_posts ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE term_taxonomy_id = 38 AND post_type IN ('product','product_variation')
GROUP BY object_id) as posts));

在删除查询之前执行此SQL查询将删除该类别产品的 meta数据。之后,可以执行删除产品的查询(所选答案的第4步)。

相关问题