我有一个像这样的数据库模型, 一个母表我们称之为 table_mother,还有几个子表。 table_mother 和 childs 之间的关系是这样的:
所有子表都有一个外键类型的名称作为母表的id(id_table_mother)(关系是1->n,因为id_table_mother是uniq并且tbale child可以获得id_table_mother的多个条目)
我想删除子表中与母表不再相关的所有记录,现在我尝试这样的事情
DELETE FROM tb_child_1,tb_child_2,tb_child_3
WHERE
tb_child_1.id_table_mother
AND tb_child_2.id_table_mother
AND tb_child_3.id_table_mother
NOT IN (SELECT id_table_mother FROM tb_table_mother);
谢谢
编辑:这就是我现在的结局
delete from tb_child_1 where id_mother not in (select id_mother from tb_mother_table);
delete from tb_child_2 where id_mother not in (select id_mother from tb_mother_table);
任何“全局”解决方案? 我的数据库也不是 innodb,所以我不能使用外键和其他东西
最佳答案
您必须构建要在删除或更新时执行的外键约束,以了解有关外键约束的更多信息,请访问 http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
关于mysql - 如何从同一查询中的多个子表中删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6136235/