MySQL“不在"询问

2021-11-20 00:00:00 sql mysql

我想运行一个简单的查询来抛出 Table1 的所有行,其中主列值不存在于另一个表 (Table2) 的列中.

I wanted to run a simple query to throw up all the rows of Table1 where a principal column value is not present in a column in another table (Table2).

我尝试使用:

SELECT * FROM Table1 WHERE Table1.principal NOT IN Table2.principal

这反而会引发语法错误.谷歌搜索把我带到论坛,那里有人说 MySQL 不支持 NOT IN 并且需要使用一些非常复杂的东西.这是真的?还是我犯了一个可怕的错误?

This is instead throwing a syntax error. Google search led me to forums where people were saying that MySQL does not support NOT IN and something extremely complex needs to be used. Is this true? Or am I making a horrendous mistake?

推荐答案

要使用 IN,必须有一个集合,请改用以下语法:

To use IN, you must have a set, use this syntax instead:

SELECT * FROM Table1 WHERE Table1.principal NOT IN (SELECT principal FROM table2)

相关文章