在 MySQL 中忽略 NULL 顯示記錄


使用 IS NOT NULL 僅顯示非 NULL 記錄。讓我們首先建立一個表 -

mysql> create table DemoTable
   -> (
   -> FirstName varchar(100)
   -> );
Query OK, 0 rows affected (3.01 sec)

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

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.44 sec)

mysql> insert into DemoTable values(NULL);
Query OK, 1 row affected (0.58 sec)

mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.31 sec)

mysql> insert into DemoTable values(NULL);
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.34 sec)

mysql> insert into DemoTable values(NULL);
Query OK, 1 row affected (0.23 sec)

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

mysql> select *from DemoTable;

輸出

這將產生以下輸出 -

+-----------+
| FirstName |
+-----------+
| John      |
| NULL      |
| Chris     |
| NULL      |
| Robert    |
| NULL      |
+-----------+
6 rows in set (0.00 sec)

以下是忽略 NULL 顯示記錄的查詢 -

mysql> select *from DemoTable where FirstName IS NOT NULL;

輸出

這將產生以下輸出 -

+-----------+
| FirstName |
+-----------+
| John      |
| Chris     |
| Robert    |
+-----------+
3 rows in set (0.02 sec)

更新於:2020 年 6 月 30 日

116 次瀏覽

開啟您的職業生涯

完成課程以獲得認證

入門
廣告
© . All rights reserved.