W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
為了處理這種情況,MySQL提供了三大運(yùn)算符:
關(guān)于 NULL 的條件比較運(yùn)算是比較特殊的。你不能使用 = NULL 或 != NULL 在列中查找 NULL 值 。
在MySQL中,NULL值與任何其它值的比較(即使是NULL)永遠(yuǎn)返回false,即 NULL = NULL 返回false 。
MySQL中處理NULL使用IS NULL和IS NOT NULL運(yùn)算符。
以下實(shí)例中假設(shè)數(shù)據(jù)庫 W3CSCHOOL 中的表 tcount_tbl 含有兩列 w3cschool_author 和 w3cschool_count, w3cschool_count 中設(shè)置插入NULL值。
嘗試以下實(shí)例:
root@host# mysql -u root -p password; Enter password:******* mysql> use W3CSCHOOL; Database changed mysql> create table tcount_tbl -> ( -> w3cschool_author varchar(40) NOT NULL, -> w3cschool_count INT -> ); Query OK, 0 rows affected (0.05 sec) mysql> INSERT INTO tcount_tbl -> (w3cschool_author, w3cschool_count) values ('mahran', 20); mysql> INSERT INTO tcount_tbl -> (w3cschool_author, w3cschool_count) values ('mahnaz', NULL); mysql> INSERT INTO tcount_tbl -> (w3cschool_author, w3cschool_count) values ('Jen', NULL); mysql> INSERT INTO tcount_tbl -> (w3cschool_author, w3cschool_count) values ('Gill', 20); mysql> SELECT * from tcount_tbl; +-----------------+----------------+ | w3cschool_author | w3cschool_count | +-----------------+----------------+ | mahran | 20 | | mahnaz | NULL | | Jen | NULL | | Gill | 20 | +-----------------+----------------+ 4 rows in set (0.00 sec) mysql>
以下實(shí)例中你可以看到 = 和 != 運(yùn)算符是不起作用的:
mysql> SELECT * FROM tcount_tbl WHERE w3cschool_count = NULL; Empty set (0.00 sec) mysql> SELECT * FROM tcount_tbl WHERE w3cschool_count != NULL; Empty set (0.01 sec)
查找數(shù)據(jù)表中 w3cschool_count 列是否為 NULL,必須使用IS NULL和IS NOT NULL,如下實(shí)例:
mysql> SELECT * FROM tcount_tbl -> WHERE w3cschool_count IS NULL; +-----------------+----------------+ | w3cschool_author | w3cschool_count | +-----------------+----------------+ | mahnaz | NULL | | Jen | NULL | +-----------------+----------------+ 2 rows in set (0.00 sec) mysql> SELECT * from tcount_tbl -> WHERE w3cschool_count IS NOT NULL; +-----------------+----------------+ | w3cschool_author | w3cschool_count | +-----------------+----------------+ | mahran | 20 | | Gill | 20 | +-----------------+----------------+ 2 rows in set (0.00 sec)
PHP腳本中你可以在 if...else 語句來處理變量是否為空,并生成相應(yīng)的條件語句。
以下實(shí)例中PHP設(shè)置了$w3cschool_count變量,然后使用該變量與數(shù)據(jù)表中的 w3cschool_count 字段進(jìn)行比較:
關(guān)于其中php的語法可以見于http://m.hgci.cn/php
isset() http://m.hgci.cn/php/php-isset.html
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if( isset($w3cschool_count ))
{
$sql = 'SELECT w3cschool_author, w3cschool_count
FROM tcount_tbl
WHERE w3cschool_count = $w3cschool_count';
}
else
{
$sql = 'SELECT w3cschool_author, w3cschool_count
FROM tcount_tbl
WHERE w3cschool_count IS $w3cschool_count';
}
mysql_select_db('W3CSCHOOL');
$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['w3cschool_author']} <br> ".
"Count: {$row['w3cschool_count']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: