MySQL NULL 值處置實例詳解。本站提示廣大學習愛好者:(MySQL NULL 值處置實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL NULL 值處置實例詳解正文
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)永遠前往false,即 NULL = NULL 前往false 。
MySQL中處置NULL運用IS NULL和IS NOT NULL運算符。
在命令提示符中運用 NULL 值
以下實例中假定數據庫 TUTORIALS 中的表 tcount_tbl 含有兩列 tutorial_author 和 tutorial_count, tutorial_count 中設置拔出NULL值。
實例
嘗試以下實例:
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>
以下實例中你可以看到 = 和 != 運算符是不起作用的:
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,必需運用IS NULL和IS NOT NULL,如下實例:
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)
運用PHP腳本處置 NULL 值
PHP腳本中你可以在 if...else 語句來處置變量能否為空,並生成相應的條件語句。
以下實例中PHP設置了$tutorial_count變量,然後運用該變量與數據表中的 tutorial_count 字段停止比擬:
<?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); ?>
感激閱讀,希望能協助到大家,謝謝大家對本站的支持!