用於比較和僅顯示具有 NULL 值的行


為此,您可以使用 IFNULL()。我們首先建立一個表格 -

mysql> create table DemoTable
(
   Value1 int,
   Value2 int
);
Query OK, 0 rows affected (0.75 sec)

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

mysql> insert into DemoTable values(10,NULL);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(20,40);
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable values(3,NULL);
Query OK, 1 row affected (0.20 sec)

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

mysql> select *from DemoTable;

這將產生以下輸出 -

+--------+--------+
| Value1 | Value2 |
+--------+--------+
|     10 | NULL   |
|     20 | 40     |
|     3 | NULL    |
+--------+--------+
3 rows in set (0.00 sec)

以下是顯示具有 NULL 值的行

mysql> select *from DemoTable where Value1 > IFNULL(Value2,0);

這將產生以下輸出 -

+--------+--------+
| Value1 | Value2 |
+--------+--------+
|     10 | NULL   |
|      3 | NULL   |
+--------+--------+
2 rows in set (0.00 sec)

更新於: 30-9 月-2019

128 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.