在本教學中,您將學習如何將NULL
值映射到其他有意義的值。
資料庫關係模型創建者 - E.F.Codd博士在關係資料庫理論中引入了NULL
概念。 根據Dr.E.C.F.Codd的表述,NULL
表示未知值或缺少資訊。
MySQL還支持NULL
表示缺少或不適用資訊的概念。
在資料庫表中,您可能會存儲包含NULL
值的數據。但是如果以報表的形式向用戶呈現數據時,顯示NULL
值是沒有意義的。
要使報告更可讀和可理解,必須顯示NULL
值作為其他有意義的值,如未知,缺失或不可用(N/A)。可以使用IF函數做到這一點。
IF
函數的語法如下:
IF(exp,exp_result1,exp_result2);
如果exp
計算結果為TRUE
(當exp <> 0
和exp <> NULL
)時,IF
函數返回exp_result1
的值,否則返回exp_result2
的值。
IF
函數的返回值可以是字串或數字,具體取決於exp_result1
和exp_result2
運算式。
讓我們練習一些例子以更好的理解。
假設要使用示例資料庫(zaixiandb)中的customers
表來作演示。
以下是customers
表中包含:customername
, state
和 country
的部分數據:
SELECT
customername, state, country
FROM
customers
ORDER BY country;
執行上面代碼,得到以下結果 -
+------------------------------------+---------------+--------------+
| customername | state | country |
+------------------------------------+---------------+--------------+
| Australian Collectors, Co. | Victoria | Australia |
| Annas Decorations, Ltd | NSW | Australia |
| Souveniers And Things Co. | NSW | Australia |
| Australian Gift Network, Co | Queensland | Australia |
|************** 此處省略了一大波數據 *********************************|
| Handji Gifts& Co | NULL | Singapore |
| Asian Shopping Network, Co | NULL | Singapore |
| SAR Distributors, Co | Pretoria | South Africa |
| Euro+ Shopping Channel | NULL | Spain |
| Diecast Collectables | MA | USA |
+------------------------------------+---------------+--------------+
122 rows in set
從上面的結果集中,您將看到state
列的值不適用於某些客戶。可以使用IF
函數將NULL
值顯示為N/A
:
SELECT
customername, IF(state IS NULL, 'N/A', state) state, country
FROM
customers
ORDER BY country;
執行上面查詢代碼,得到以下結果 -
+------------------------------------+---------------+--------------+
| customername | state | country |
+------------------------------------+---------------+--------------+
| Australian Collectors, Co. | Victoria | Australia |
| Annas Decorations, Ltd | NSW | Australia |
| Souveniers And Things Co. | NSW | Australia |
| Australian Gift Network, Co | Queensland | Australia |
| Australian Collectables, Ltd | Victoria | Australia |
| Salzburg Collectables | N/A | Austria |
| Mini Auto Werke | N/A | Austria |
| Petit Auto | N/A | Belgium |
| Royale Belge | N/A | Belgium |
|************** 此處省略了一大波數據 *********************************|
| Motor Mint Distributors Inc. | PA | USA |
| Signal Collectibles Ltd. | CA | USA |
| Diecast Collectables | MA | USA |
+------------------------------------+---------------+--------------+
122 rows in set
除了IF
函數外,MySQL提供了IFNULL函數,可以直接處理NULL
值。 以下是IFNULL
函數的語法:
IFNULL(exp,exp_result);
重寫上代碼查詢 -
SELECT customername,
IFNULL(state,"N/A") state,
country
FROM customers
ORDER BY country;
如果exp
計算結果為NULL
值,則IFNULL
函數返回exp_result
運算式的值,否則返回exp
運算式的值。
以下查詢使用IFNULL
函數將NULL
顯示為未知,如下所示:
SELECT customername,
IFNULL(state,"N/A") state,
country
FROM customers
ORDER BY country;
執行上面查詢代碼,得到以下結果 -
+------------------------------------+---------------+--------------+
| customername | state | country |
+------------------------------------+---------------+--------------+
| Australian Collectors, Co. | Victoria | Australia |
| Annas Decorations, Ltd | NSW | Australia |
| Souveniers And Things Co. | NSW | Australia |
| Australian Gift Network, Co | Queensland | Australia |
| Australian Collectables, Ltd | Victoria | Australia |
| Salzburg Collectables | N/A | Austria |
| Mini Auto Werke | N/A | Austria |
| Petit Auto | N/A | Belgium |
| Royale Belge | N/A | Belgium |
|************** 此處省略了一大波數據 *********************************|
| Motor Mint Distributors Inc. | PA | USA |
| Signal Collectibles Ltd. | CA | USA |
| Diecast Collectables | MA | USA |
+------------------------------------+---------------+--------------+
122 rows in set
在本教學中,您已經學習了如何使用IF
和IFNULL
函數將NULL
值映射到其他更有意義的值,以便以可讀方式呈現數據。