如何在 MySQL 中選擇沒有 NULL 記錄的資料?


要選擇非空記錄,請使用 IS NOT NULL 屬性。讓我們首先建立一個表 -

mysql> create table DemoTable1792
     (
     Name varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

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

mysql> insert into DemoTable1792 values('John Smith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1792 values(NULL);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1792 values('David Miller');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1792 values(NULL);
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from DemoTable1792;

這將產生以下輸出 -

+--------------+
| Name         |
+--------------+
| John Smith   |
| NULL         |
| David Miller |
| NULL         |
+--------------+
4 rows in set (0.00 sec)

以下是選擇不帶有任何空記錄的資料的查詢 -

mysql> select * from DemoTable1792 where Name IS NOT NULL;

這將產生以下輸出 -

+--------------+
| Name         |
+--------------+
| John Smith   |
| David Miller |
+--------------+
2 rows in set (0.00 sec)

更新於:2019-12-23

79 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.