我在许多表格中都有产品数据。 我想从他们的表格中删除所有这些数据product_id。但不想使用很多查询。
例如
1. delete from tbl_product_attributes where product_id = 'this'
2. delete from tbl_product_barcode where product_id = 'this'
3. delete from tbl_product_images where product_id = 'this'
4. delete from tbl_product where product_id = 'this'
我只想要一个查询,从数据库中删除特定给定product_id的所有相对数据。 就像这样
Delete data from whole DB where product_id = 'this'.
注意:而且此查询不会对我的服务器造成负担。
有什么办法吗?
你可以这样尝试:
DELETE T1, T2
FROM T1
INNER JOIN T2 ON T1.key = T2.key
WHERE condition;
此外,还可以使用 ON DELETE CASCADE 选项在表上定义外键约束。
然后,从父表中删除记录会从子表中删除记录。
检查此链接:Mysql删除