在本教學中,您將學習如何將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值映射到其他更有意義的值,以便以可讀方式呈現數據。
