在 MySQL 中顯示除某一以外的所有記錄


您可以使用 IN() 在 MySQL 中顯示除某一以外的所有記錄。我們首先建立一個表 −

mysql> create table DemoTable
(
   Id int,
   FirstName varchar(20)
);
Query OK, 0 rows affected (0.57 sec)

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

mysql> insert into DemoTable values(100,'Larry');
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable values(10,'Chris');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values(110,'Robert');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(90,'David');
Query OK, 1 row affected (0.20 sec)

以下是使用 select 語句從表中顯示所有記錄的查詢 −

mysql> select *from DemoTable;

這將生成以下輸出 −

+------+-----------+
| Id   | FirstName |
+------+-----------+
| 100  | Larry     |
| 10   | Chris     |
| 110  | Robert    |
| 90   | David     |
+------+-----------+
4 rows in set (0.00 sec)

以下是顯示 MySQL 中除某一以外的所有記錄的查詢 −

mysql> select *from DemoTable where Id IN(110,90,100);

這將生成以下輸出 −

+------+-----------+
| Id   | FirstName |
+------+-----------+
| 100  | Larry     |
| 110  | Robert    |
| 90   | David     |
+------+-----------+
3 rows in set (0.00 sec)

更新於:30-Jul-2019

478 次瀏覽

開啟你的 職業生涯

完成課程,拿取認證

開始
廣告
© . All rights reserved.