如何檢查 MySQL 中的資料是否為 NULL?
您可以使用 IF() 檢查資料是否為 NULL。第一,讓我們建立一個表 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(200), Age int ); Query OK, 0 rows affected (0.44 sec)
使用 insert 命令在表中插入記錄 -
mysql> insert into DemoTable(Name,Age) values('John',23);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(Name,Age) values('Sam',null);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(Name,Age) values('Mike',23);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(Name,Age) values('David',21);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable(Name,Age) values('Carol',null);
Query OK, 1 row affected (0.13 sec)使用 select 命令從表中顯示記錄 -
mysql> select *from DemoTable;
這將生成以下輸出 -
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | John | 23 | | 2 | Sam | NULL | | 3 | Mike | 23 | | 4 | David | 21 | | 5 | Carol | NULL | +----+-------+------+ 5 rows in set (0.00 sec)
以下查詢可檢查資料是否為 NULL,它會在記錄中可見的 NULL 值處新增訊息 -
mysql> select if(Age IS NULL,'Age is missing',Age) from DemoTable;
這將生成以下輸出 -
+--------------------------------------+ | if(Age IS NULL,'Age is missing',Age) | +--------------------------------------+ | 23 | | Age is missing | | 23 | | 21 | | Age is missing | +--------------------------------------+ 5 rows in set (0.00 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP