MySQL NULL 值處理
我們已經知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句來讀取資料表中的資料,但是當提供的查詢條件欄位為 NULL 時,該命令可能就無法正常工作。
為了處理這種情況,MySQL提供了三大運算子:
- IS NULL: 當列的值是 NULL,此運算子返回 true。
- IS NOT NULL: 當列的值不為 NULL, 運算子返回 true。
- <=>: 比較操作符(不同於 = 運算子),當比較的的兩個值相等或者都為 NULL 時返回 true。
關於 NULL 的條件比較運算是比較特殊的。你不能使用 = NULL 或 != NULL 在列中查詢 NULL 值 。
在 MySQL 中,NULL 值與任何其它值的比較(即使是 NULL)永遠返回 NULL,即 NULL = NULL 返回 NULL 。
MySQL 中處理 NULL 使用 IS NULL 和 IS NOT NULL 運算子。
注意:
select * , columnName1+ifnull(columnName2,0) from tableName;
columnName1,columnName2 為 int 型,當 columnName2 中,有值為 null 時,columnName1+columnName2=null,
ifnull(columnName2,0) 把 columnName2 中 null 值轉為 0。
在命令提示符中使用 NULL 值
以下例項中假設資料庫 itread01 中的表 itread01_test_tbl 含有兩列 itread01_author 和 itread01_count, itread01_count 中設定插入NULL值。
例項
嘗試以下例項:
建立資料表 itread01_test_tbl
root@host
Enter password:*******
mysql> use itread01;
Database changed
mysql> create table itread01_test_tbl
-> (
-> itread01_author varchar(40) NOT NULL,
-> itread01_count INT
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO itread01_test_tbl (itread01_author, itread01_count) values ('itread01', 20);
mysql> INSERT INTO itread01_test_tbl (itread01_author, itread01_count) values ('入門教學', NULL);
mysql> INSERT INTO itread01_test_tbl (itread01_author, itread01_count) values ('Google', NULL);
mysql> INSERT INTO itread01_test_tbl (itread01_author, itread01_count) values ('FK', 20);
mysql> SELECT * from itread01_test_tbl;
+---------------+--------------+
| itread01_author | itread01_count |
+---------------+--------------+
| itread01 | 20 |
| 入門教學 | NULL |
| Google | NULL |
| FK | 20 |
+---------------+--------------+
4 rows in set (0.01 sec)
以下例項中你可以看到 = 和 != 運算子是不起作用的:
mysql> SELECT * FROM itread01_test_tbl WHERE itread01_count = NULL;
Empty set (0.00 sec)
mysql> SELECT * FROM itread01_test_tbl WHERE itread01_count != NULL;
Empty set (0.01 sec)
查詢資料表中 itread01_test_tbl 列是否為 NULL,必須使用 IS NULL 和 IS NOT NULL,如下例項:
mysql> SELECT * FROM itread01_test_tbl WHERE itread01_count IS NULL;
+---------------+--------------+
| itread01_author | itread01_count |
+---------------+--------------+
| 入門教學 | NULL |
| Google | NULL |
+---------------+--------------+
2 rows in set (0.01 sec)
mysql> SELECT * from itread01_test_tbl WHERE itread01_count IS NOT NULL;
+---------------+--------------+
| itread01_author | itread01_count |
+---------------+--------------+
| itread01 | 20 |
| FK | 20 |
+---------------+--------------+
2 rows in set (0.01 sec)
使用 PHP 指令碼處理 NULL 值
PHP 指令碼中你可以在 if...else 語句來處理變數是否為空,並生成相應的條件語句。
以下例項中 PHP 設定了 $itread01_count 變數,然後使用該變數與資料表中的 itread01_count 欄位進行比較:
MySQL ORDER BY 測試:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '123456';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('連線失敗: ' . mysqli_error($conn));
}
mysqli_query($conn , "set names utf8");
if( isset($itread01_count ))
{
$sql = "SELECT itread01_author, itread01_count
FROM itread01_test_tbl
WHERE itread01_count = $itread01_count";
}
else
{
$sql = "SELECT itread01_author, itread01_count
FROM itread01_test_tbl
WHERE itread01_count IS NULL";
}
mysqli_select_db( $conn, 'itread01' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('無法讀取資料: ' . mysqli_error($conn));
}
echo '<h2>入門教學 IS NULL 測試<h2>';
echo '<table border="1"><tr><td>作者</td><td>登陸次數</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
echo "<tr>".
"<td>{$row['itread01_author']} </td> ".
"<td>{$row['itread01_count']} </td> ".
"</tr>";
}
echo '</table>';
mysqli_close($conn);
?>