MySql delete多表关联删除的使用方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Alger_charset/article/details/79686587

假设条件

假设有三张表a,b,c,三张表之间有关联关系,在删除一张表中的一行数据的时候需要同时删除其他表中的对应的数据,或是删除表中的数据需要通过另外的表中的条件才能知道,这时候如何能有sql语句快速删除呢?

解决方案

– 1)语句1
delete c from a inner join b on a.join_code = b.join_code inner join c on b.store_id = c.store_id
where a.join_code=’123456’
这条语句假设a,b通过join_code关联,b,c通过store_id关联,实现删除c表中的数据,需要通过a表中的join_code知道c表中的对应数据,where条件查出几条就会从c表删除几条
– 2)语句2
delete a,b,c from a inner join b on a.join_code = b.join_code inner join c on b.store_id = c.store_id where a.join_code=’123456’
这条语句假设a,b通过join_code关联,b,c通过store_id关联,实现删除三个表数据,需要通过a表中的join_code知道三个表中的对应数据,where条件查出几条就会从三个表各删除几条

猜你喜欢

转载自blog.csdn.net/Alger_charset/article/details/79686587