[英]SQL Delete all rows in a table based on another table and another column value
table1:表格1:
| ID | POST STATUS |
| 12807 | wc-refunded |
| 13075 | wc-refunded |
table2:表2:
| ORDER ID | ------- |
| 12807 | ------- |
| 13075 | ------- |
Let me explain better, i want to delete all ORDER ID in table 2, based on all ID of table 1 if post_status = wc-refunded让我更好地解释一下,如果 post_status = wc-refunded,我想根据表 1 的所有 ID 删除表 2 中的所有 ORDER ID
MYSQL VERSION: 5.7 MYSQL 版本:5.7
EDIT : fixed with first solution from the @Tim Biegeleisen ( THANKS! )编辑:用@Tim Biegeleisen 的第一个解决方案修复(谢谢! )
DELETE
FROM wp_woocommerce_downloadable_product_permissions
WHERE EXISTS (SELECT 1 FROM wp_posts
WHERE wp_posts.ID = wp_woocommerce_downloadable_product_permissions.order_id AND wp_posts.post_status = 'wc-refunded');
You could use exists logic:您可以使用存在逻辑:
DELETE
FROM table2 t2
WHERE EXISTS (SELECT 1 FROM table1 t1
WHERE t1.ID = t2.ORDERID AND t1.poststatus = 'wc-refunded');
We can also try using delete join logic:我们也可以尝试使用删除连接逻辑:
DELETE t2
FROM table2 t2
INNER JOIN table1 t1
ON t1.ID = t2.ORDERID
WHERE t1.poststatus = 'wc-refunded';
Try this one:试试这个:
DELETE FROM table2
WHERE OrderID IN (
SELECT DISTINCT ID
FROM table1
WHERE PostStatus = 'wc-refunded'
)
It is a bit resource intensive, but should work...它有点资源密集型,但应该可以工作......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.