php - 使用具有外键的内连接删除

标签 php mysql

我需要一些有关查询的帮助。

这些是我的表格:

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/

相关文章:

javascript - 将 PHP/SQL 变量添加到 Bootbox 文本区域

mysql - 用于数据库设计的抽象/超/子类结构

mysql - 将 select 的结果返回到一个变量 MYSQL

php - 从特定类别 codeigniter 中选择产品

mysql - 更新mysql View 内的查询

php - For 循环的最后一次迭代,如果条件与 Twig

php - escape_string 和 real_escape_string 有什么区别?

php - 在另一个查询中使用查询结果

php - Mysql PHP,检索字符串包含撇号无法替换

mysql - SQL 连接性能注意事项