在包含空值的欄位中執行 MySQL SELECT?


要檢查 SELECT 中的 NULL 值,請使用 MySQL NULL。我們首先建立一個表 −

mysql> create table DemoTable1455
   -> (
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.47 sec)

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

mysql> insert into DemoTable1455 values('John');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable1455 values(NULL);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1455 values('');
Query OK, 1 row affected (0.19 sec)

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

mysql> select * from DemoTable1455;

這將產生以下輸出 −

+------+
| Name |
+------+
| John |
| NULL |
|      |
+------+
3 rows in set (0.00 sec)

以下是針對包含空值的欄位執行 MySQL 選擇的查詢 −

mysql> select * from DemoTable1455 where Name <> 'John' or Name is null;

這將產生以下輸出 −

+------+
| Name |
+------+
| NULL |
|      |
+------+
2 rows in set (0.00 sec)

更新於:2019-12-10

66 次瀏覽

開啟您的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.