使用 MySQL 從不存在 value 的表中選擇?


為此,您可以使用 NOT IN() −

mysql> create table DemoTable1991
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(20)
);
Query OK, 0 rows affected (0.61 sec)

使用 insert 命令在表中插入一些記錄 −

mysql> insert into DemoTable1991(StudentName) values('Chris');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable1991(StudentName) values('Bob');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1991(StudentName) values('David');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable1991(StudentName) values('Sam');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1991(StudentName) values('Mike');
Query OK, 1 row affected (0.11 sec)

使用 select 語句顯示錶中的所有記錄 −

mysql> select * from DemoTable1991;

這將產生以下輸出 −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|         1 | Chris       |
|         2 | Bob         |
|         3 | David       |
|         4 | Sam         |
|         5 | Mike        |
+-----------+-------------+
5 rows in set (0.03 sec)

以下是從表中選擇 * 的查詢,其中不存在 value

mysql> select * from DemoTable1991
   where StudentName NOT IN('Bob','Sam','Mike');

這將產生以下輸出 −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|         1 | Chris       |
|         3 | David       |
+-----------+-------------+
2 rows in set (0.04 sec)

更新於:02-Jan-2020

709 瀏覽量

啟動你的 職業

完成課程並獲得證書

開始學習
廣告