从整个数据库中删除product_id的数据

时间:2018-10-03 08:23:58

标签: php mysql codeigniter sql-delete

我在许多表格中都有产品数据。我想从其表中通过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'.

注意:而且此查询也不会对我的服务器造成负担。

有什么办法吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试这样:

DELETE T1, T2
FROM T1
INNER JOIN T2 ON T1.key = T2.key
WHERE condition;

此外,您还可以使用ON DELETE CASCADE选项在表上定义外键约束。

然后从父表中删除记录将从子表中删除记录。

检查此链接:Mysql delete