SQL NOT

SQL NOT 操作符用来对一个表达式的结果进行否定。

SQL NOT 真值表:

表达式值 NOT
True False
False True
Unknown Unknown

SQL NOT 语法

SELECT * 
FROM TableName
WHERE NOT predicate

SQL NOT 范例

数据表: Employees

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
204 Jozzh Lnanyo Finance 45800
205 Syllauu Dfaafk Finance 57000
206 Gecrrcc Srlkrt 62000
302 Jssme Bdnaa Development 75000
303 Dnnaao Errllov Development 55000
304 Jyoysww Osk 49000

例1: SQL NOT IN
选择所有LastName不是Lnanyo, Srlkrt 或 Osk 的职员(employee):

SELECT * 
FROM Employees
WHERE NOT LastName IN ('Lnanyo', 'Srlkrt', 'Osk')

我们也可以这样写:

SELECT * 
FROM Employees
WHERE LastName NOT IN ('Lnanyo', 'Srlkrt', 'Osk')

结果会类似于:

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
205 Syllauu Dfaafk Finance 57000
302 Jssme Bdnaa Development 75000
303 Dnnaao Errllov Development 55000

例2: SQL NOT LIKE
选择所有FirstName不是以”J”开始的职员(employee):

SELECT * 
FROM Employees
WHERE NOT FirstName LIKE 'J%'

我们也可以这样写:

SELECT * 
FROM Employees
WHERE FirstName NOT LIKE 'J%'

结果会类似于:

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
205 Syllauu Dfaafk Finance 57000
206 Gecrrcc Srlkrt 62000
303 Dnnaao Errllov Development 55000

例3: SQL IS NOT NULL
选择所有为某个部门成员的职员:

SELECT * 
FROM Employees
WHERE NOT Department IS NULL

我们也可以这样写:

SELECT * 
FROM Employees
WHERE Department IS NOT NULL

结果会类似于:

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
204 Jozzh Lnanyo Finance 45800
205 Syllauu Dfaafk Finance 57000
302 Jssme Bdnaa Development 75000
303 Dnnaao Errllov Development 55000