在本教學中,您將學習如何使用SQL NOT
運算子來否定SELECT
語句WHERE
子句中的布林表示式。
在前面已經學習了如何使用各種邏輯運算子,如:AND,OR,LIKE,BETWEEN,IN和EXISTS。 這些運算子可幫助您在WHERE子句中形成靈活的條件。
要反轉任何布林表示式的結果,請使用NOT
運算子。 以下演示如何使用NOT
運算子。
NOT [Boolean_expression]
下表顯示了NOT
運算子的結果。
原值 | 應用Not運算子後 |
---|---|
TRUE | FALSE |
FALSE | TRUE |
NULL | NULL |
我們將使用employees
表來演示NOT
運算子。employees
表的結構如下所示 -
mysql> DESC employees;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| employee_id | int(11) | NO | PRI | NULL | auto_increment |
| first_name | varchar(20) | YES | | NULL | |
| last_name | varchar(25) | NO | | NULL | |
| email | varchar(100) | NO | | NULL | |
| phone_number | varchar(20) | YES | | NULL | |
| hire_date | date | NO | | NULL | |
| job_id | int(11) | NO | MUL | NULL | |
| salary | decimal(8,2) | NO | | NULL | |
| manager_id | int(11) | YES | MUL | NULL | |
| department_id | int(11) | YES | MUL | NULL | |
+---------------+--------------+------+-----+---------+----------------+
10 rows in set
以下語句檢索在部門ID為5
中工作的所有員工。如下查詢語句 -
SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
department_id = 5
ORDER BY
salary;
執行上面範例程式碼,得到以下結果 -
查詢工作在部門ID為5
且薪水不超過5000
的員工。
SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
department_id = 5
AND NOT salary > 5000
ORDER BY
salary;
執行上面範例程式碼,得到以下結果 -
+-------------+------------+-----------+--------+
| employee_id | first_name | last_name | salary |
+-------------+------------+-----------+--------+
| 126 | Irene | Liu | 2700 |
| 193 | Britney | Zhao | 3900 |
| 192 | Sarah | Yang | 4000 |
+-------------+------------+-----------+--------+
3 rows in set
SQL NOT IN運算子範例
要否定IN
運算子,請使用NOT
運算子。 例如,以下語句將獲取不在部門ID為1
,2
或3
中工作的所有員工。
SELECT
employee_id,
first_name,
last_name,
department_id
FROM
employees
WHERE
department_id NOT IN (1, 2, 3)
ORDER BY
first_name;
執行上面查詢語句,得到以下結果 -
+-------------+------------+-----------+---------------+
| employee_id | first_name | last_name | department_id |
+-------------+------------+-----------+---------------+
| 103 | Alexander | Lee | 6 |
| 193 | Britney | Zhao | 5 |
| 104 | Bruce | Wong | 6 |
| 179 | Charles | Yang | 8 |
| 109 | Daniel | Chen | 10 |
| 105 | David | Liang | 6 |
| 107 | Diana | Chen | 6 |
| 204 | Hermann | Wu | 7 |
| 126 | Irene | Liu | 5 |
......
| 100 | Steven | Lee | 9 |
| 203 | Susan | Zhou | 4 |
| 106 | Valli | Chen | 6 |
| 206 | William | Wu | 11 |
+-------------+------------+-----------+---------------+
31 rows in set
SQL NOT LIKE運算子範例
可以使用NOT LIKE
來否定LIKE運算子。 例如,以下語句檢索名字不以字母M
開頭的所有員工。
SELECT
first_name,
last_name
FROM
employees
WHERE
first_name NOT LIKE 'M%'
ORDER BY
first_name;
執行上面查詢語句,得到以下結果 -
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| Alexander | Lee |
| Alexander | Su |
| Avg | Su |
| Britney | Zhao |
| Bruce | Wong |
| Charles | Yang |
... ...
| Shelley | Wu |
| Shelli | Zhang |
| Sigal | Zhang |
| Steven | Lee |
| Susan | Zhou |
| Valli | Chen |
| William | Wu |
+------------+-----------+
34 rows in set
SQL NOT BETWEEN範例
以下範例說明如何使用NOT
來否定BETWEEN運算子,以使員工的薪水不在1000
到1000
之間。
SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
salary NOT BETWEEN 3000
AND 5000
ORDER BY
salary;
執行上面查詢語句,得到以下結果 -
+-------------+------------+-----------+--------+
| employee_id | first_name | last_name | salary |
+-------------+------------+-----------+--------+
| 119 | Karen | Zhang | 2500 |
| 118 | Guy | Zhang | 2600 |
| 126 | Irene | Liu | 2700 |
| 117 | Sigal | Zhang | 2800 |
| 116 | Shelli | Zhang | 2900 |
| 104 | Bruce | Wong | 6000 |
| 202 | Pat | Zhou | 6000 |
| 179 | Charles | Yang | 6200 |
... ...
| 205 | Shelley | Wu | 12000 |
| 201 | Michael | Zhou | 13000 |
| 146 | Karen | Liu | 13500 |
| 145 | John | Liu | 14000 |
| 102 | Lex | Liang | 17000 |
| 101 | Neena | Wong | 17000 |
| 100 | Steven | Lee | 24000 |
+-------------+------------+-----------+--------+
33 rows in set
SQL NOT EXISTS範例
請參考以下員工和家屬表:
以下查詢使用NOT EXISTS
運算子來獲取沒有任何家屬的員工。
SELECT
employee_id, first_name, last_name
FROM
employees e
WHERE
NOT EXISTS (
SELECT
employee_id
FROM
dependents d
WHERE
d.employee_id = e.employee_id
);
執行上面查詢語句,得到以下結果 -
通過上面的學習,現在您應該知道如何使用NOT
運算子來否定布林表示式了。