前面已經(jīng)介紹了如何利用 SQL 的 SELECT 命令配合 WHERE 子句來(lái)獲取 MySQL 表中的數(shù)據(jù),但假如嘗試給出一個(gè)條件,將字段或列值與 NULL 比對(duì),則會(huì)出現(xiàn)異常。
為了處理這種情況,MySQL 提供了三種運(yùn)算符:
=
運(yùn)算符不同)返回 true。 包含 NULL 的條件都是比較特殊的。不能在列中使用 = NULL 或 ! = NULL 來(lái)尋找 NULL 值。這樣的比對(duì)通常都是失敗的,因?yàn)椴豢赡艿弥@樣的比對(duì)是否為真。即使 NULL = NULL 失敗。
要想確定列是否為 NULL,要使用 IS NULL 或 IS NOT NULL。
假設(shè)數(shù)據(jù)庫(kù) TUTORIALS 中包含一張叫做 tcount_tbl 的表,這張表包含兩列 tutorial_author 與 tutorial_count,則 tutorial_count 中出現(xiàn)的 NULL 值代表該字段值未知。
請(qǐng)看下面這個(gè)范例:
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> create table tcount_tbl
-> (
-> tutorial_author varchar(40) NOT NULL,
-> tutorial_count INT
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO tcount_tbl
-> (tutorial_author, tutorial_count) values ('mahran', 20);
mysql> INSERT INTO tcount_tbl
-> (tutorial_author, tutorial_count) values ('mahnaz', NULL);
mysql> INSERT INTO tcount_tbl
-> (tutorial_author, tutorial_count) values ('Jen', NULL);
mysql> INSERT INTO tcount_tbl
-> (tutorial_author, tutorial_count) values ('Gill', 20);
mysql> SELECT * from tcount_tbl;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran | 20 |
| mahnaz | NULL |
| Jen | NULL |
| Gill | 20 |
+-----------------+----------------+
4 rows in set (0.00 sec)
mysql>
下面,你會(huì)發(fā)現(xiàn) = 和 != 并不適用于 NULL 值。
mysql> SELECT * FROM tcount_tbl WHERE tutorial_count = NULL;
Empty set (0.00 sec)
mysql> SELECT * FROM tcount_tbl WHERE tutorial_count != NULL;
Empty set (0.01 sec)
所以,要想確定 tutorial_count 列中到底何值為 NULL,何值不為 NULL,查詢應(yīng)該這樣寫:
mysql> SELECT * FROM tcount_tbl
-> WHERE tutorial_count IS NULL;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahnaz | NULL |
| Jen | NULL |
+-----------------+----------------+
2 rows in set (0.00 sec)
mysql> SELECT * from tcount_tbl
-> WHERE tutorial_count IS NOT NULL;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran | 20 |
| Gill | 20 |
+-----------------+----------------+
2 rows in set (0.00 sec)
可以使用 if...else
條件語(yǔ)句來(lái)準(zhǔn)備一個(gè)基于 NULL 值的查詢。
下面這個(gè)例子獲取外部的 tutorial_count,并將其與表中的值進(jìn)行比對(duì)。
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if( isset($tutorial_count ))
{
$sql = 'SELECT tutorial_author, tutorial_count
FROM tcount_tbl
WHERE tutorial_count = $tutorial_count';
}
else
{
$sql = 'SELECT tutorial_author, tutorial_count
FROM tcount_tbl
WHERE tutorial_count IS $tutorial_count';
}
mysql_select_db('TUTORIALS');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Author:{$row['tutorial_author']} <br> ".
"Count: {$row['tutorial_count']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>