我需要一些有关查询的帮助。
这些是我的表格:
Table 1 - General
id
name
last_name
<小时/>
Table 2 - User
id
username
table1_Id (This one references to table1 ids) - FK
由于我的引用是级联的,如果我删除 table1,它将删除所有其他引用。
但我不知道如何进行查询。
我想先获取Table2中的Id,然后获取Table2.Table1_id,然后转到Table1并删除我刚刚获取的Id。
最佳答案
delete from Table1
你的键是级联的,这也会删除 Table2 中的所有行
您可以像平常一样从 table1 中删除一行
delete from Table1 where id=to_delete
这将级联并删除 table2 中引用 to_delete
的行
好的;再次编辑
delete from Table1 where id=(select table1_id from Table2 where id=group_to_delete)
将从 Table1 中删除带有从 Table2 中的行获取的 id 的行,这将级联并删除 Table2 中共享相同键的行。
关于php - 使用具有外键的内连接删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813604/