先骂一顿csdn吧,虽然最开始是在csdn上写的博客,而且从csdn上查到了很多资料,但是csdn使用实在是差劲。账号管理莫名其妙,有的时候把我的id和qq号搞混了;有时候经常发不了博文,无法点击。算了,不说了。
不记得之前是解决什么问题了,用到了很复杂的一条语句,在这大概描述下。
问题:一条sql语句同时删除两个表中相同记录
1.精简版的
delete a,b from table_a a,table_b b where a.id=b.id;
2.复杂版的
delete a,b from table_a a,table_b b where a.id in(select c.id from (select * from table_a a where a.id in (select b.id from table_b b group by b.id having count(*) > 0)) c);
上述两种结果是一样的。
第二种虽然复杂,但是有些场合是可以使用的。
此外还有触发器版的,
create trigger tr_name on table1
for delete
as
delete from table2 where id in (select id from deleted)
然后删除
delete table1 where id条件。挺精巧的,可以仔细理解下。