最近的做的项目用到数据库的联表查询和联表删除,因为table2中可能没有table1的匹配记录,所以用到了外连接。废话不多说,上sql:
delete tb1,tb2,tb3,tb4,tb5,tb6
from tb1
left join tb2 on (tb1.id = tb2.id)
left join tb3 on (tb1.id = tb3.id)
left join tb4 on (tb3.id = tb4.id)
left join tb5 on (tb1.id = tb5.id)
left join tb6 on (tb6.id = tb3.id)
where tb1.id >10;
此处特别注意,delete关键字和where关键字后面不要用表的别名(此处表都没有起别名),否则会报语法错误!
原创文章,转载请注明出处,谢谢!