您的位置:首页 > 数据库 > MySQL

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

2018-03-25 14:49 513 查看

假设条件

假设有三张表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条件查出几条就会从三个表各删除几条
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: