Answers:
这是什么领域?IN运算符不能与单个字段一起使用,而应与子查询或预定义列表一起使用:
-- subquery
SELECT a FROM x WHERE x.b NOT IN (SELECT b FROM y);
-- predefined list
SELECT a FROM x WHERE x.b NOT IN (1, 2, 3, 6);
如果要搜索字符串,请使用LIKE运算符(但这会很慢):
-- Finds all rows where a does not contain "text"
SELECT * FROM x WHERE x.a NOT LIKE '%text%';
如果将其限制为要搜索的字符串必须以给定的字符串开头,则可以使用索引(如果该字段上有索引)并且速度相当快:
-- Finds all rows where a does not start with "text"
SELECT * FROM x WHERE x.a NOT LIKE 'text%';
NOT IN
确保所有值都不为NULL,因为如果您不熟悉三值逻辑,则NOT IN和NULL不会以明显的方式组合。在这里您将使用SELECT a FROM x WHERE x.b NOT IN (SELECT b FROM y WHERE b IS NOT NULL);
如果还需要排除NULL值,则需要执行以下操作:SELECT a FROM x WHERE x.b NOT IN (SELECT b FROM y WHERE b IS NOT NULL) AND x.b IS NOT NULL;