Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用JOIN语句从表中删除?_Mysql - Fatal编程技术网

Mysql 如何使用JOIN语句从表中删除?

Mysql 如何使用JOIN语句从表中删除?,mysql,Mysql,我发现的例子中没有一个在我的案例中起作用。我有两张桌子: 项目带有类别id;和 目录带有id 我想从项目中删除所有不在目录中的记录 谢谢 您可以使用left-join进行删除,如其他一些答案中所示,但这一方法同样有效,并且更适合于其他不总是支持delete语句中的left-join构造的数据库 delete i from items i left join catalog c on c.id = i.cat_id where c.id is null delete from items whe

我发现的例子中没有一个在我的案例中起作用。我有两张桌子:

项目
带有
类别id
;和
目录
带有
id

我想从
项目
中删除所有不在
目录
中的记录


谢谢

您可以使用left-join进行删除,如其他一些答案中所示,但这一方法同样有效,并且更适合于其他不总是支持delete语句中的left-join构造的数据库

delete i
from items i
left join catalog c on c.id = i.cat_id
where c.id is null
delete from items
where cat_id not in (select id from catalog)

顺便说一下,如果您有适当的外键约束,则不可能存在引用不存在的类别的项。我认为你应该考虑把这些限制放在适当位置。< / P>什么例子?例如,官方手册中的手册通常起着很好的作用。
DELETE FROM items i
LEFT JOIN catalog c ON i.cat_id = c.id 
WHERE c.id IS NULL;