從列表中排除一些 ID 記錄,並在 MySQL 中顯示其他記錄


要排除記錄,請使用 MySQL NOT IN()。我們首先建立一個表 -

mysql> create table DemoTable
(
   Id int
);
Query OK, 0 rows affected (0.64 sec)

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

mysql> insert into DemoTable values(1);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(2);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(3);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values(4);
Query OK, 1 row affected (0.41 sec)
mysql> insert into DemoTable values(5);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values(6);
Query OK, 1 row affected (0.12 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

將產生以下輸出 -

+------+
| Id   |
+------+
| 1    |
| 2    |
| 3    |
| 4    |
| 5    |
| 6    |
+------+
6 rows in set (0.00 sec)

以下是排除記錄的查詢 -

mysql> select *from DemoTable where Id NOT IN(1,4,6,3);

將產生以下輸出 -

+------+
| Id   |
+------+
| 2    |
| 5    |
+------+
2 rows in set (0.00 sec)

更新於:2019 年 10 月 9 日

690 次瀏覽

開啟您的 事業

取得該課程認證

開始
廣告
© . All rights reserved.