MySQL NULL 值處理實例教學

零下一度
發布: 2017-05-15 10:31:24
原創
1361 人瀏覽過

MySQL NULL值處理

我們已經知道MySQL使用SQL SELECT指令和WHERE子句來讀取資料表中的數據,但是當提供的查詢條件欄位為NULL時,該指令可能無法正常運作。

為了處理這種情況時,MySQL提供了三大運算子:

IS NULL:當資料列的值為NULL,此運算子傳回true。

IS NOT NULL:當列的值不為NULL,運算子回傳true。

<=>:  比較運算子(不同於=運算子),當比較的的兩個值為NULL時傳回真。

關於NULL的條件比較運算是比較特殊的。你不能使用= NULL或! = NULL在列中尋找NULL值。

在MySQL中,NULL值與任何其它值的比較(即使是NULL)永遠回傳false,即NULL = NULL回傳false。

MySQL中處理NULL使用IS NULL和IS NOT NULL運算子。

在命令提示字元中使用NULL值

以下實例中假設資料庫指南中的表tcount_tbl包含兩列tutorial_author和tutorial_count,tutorial_count中設定插入NULL值。

嘗試以下實例:

root @ host#mysql -u root -p password;
输入密码:*******
mysql> use TUTORIALS;数据库已更改mysql> create table tcount_tbl
    - >(
    - > tutorial_author varchar(40)NOT NULL,
    - > tutorial_count INT
    - >);
查询OK,0行受影响(0.05秒)
mysql> INSERT INTO tcount_tbl
    - >(tutorial_author,tutorial_count)值(&#39;mahran&#39;,20);
mysql> INSERT INTO tcount_tbl
    - >(tutorial_author,tutorial_count)values(&#39;mahnaz&#39;,NULL);
mysql> INSERT INTO tcount_tbl
    - >(tutorial_author,tutorial_count)值(&#39;Jen&#39;,NULL);
mysql> INSERT INTO tcount_tbl
    - >(tutorial_author,tutorial_count)值(&#39;Gill&#39;,20);
mysql> select * from tcount_tbl;
+ ----------------- + ---------------- +
| tutorial_author | tutorial_count |
+ ----------------- + ---------------- +
| 马赫兰 20 |
| mahnaz | NULL |
| 仁| NULL |
| 鳃| 20 |
+ ----------------- + ---------------- +
4行(0.00秒)
MySQL的>
登入後複製

以下實例中你可以看到=和! =運算子是不起作用的

mysql> SELECT * FROM tcount_tbl WHERE tutorial_count = NULL;
空置(0.00秒)
mysql> SELECT * FROM tcount_tbl WHERE tutorial_count!= NULL;
空置(0.01秒)
登入後複製

查詢資料表中tutorial_count列是否為NULL,必須使用IS NULL和IS NOT NULL,如下實例:

mysql> SELECT * FROM tcount_tbl 
    - > WHERE tutorial_count IS NULL;
+ ----------------- + ---------------- +
| tutorial_author | tutorial_count |
+ ----------------- + ---------------- +
| mahnaz | NULL |
| 仁| NULL |
+ ----------------- + ---------------- +
2行(0.00秒)
mysql> select * from tcount_tbl 
    - > WHERE tutorial_count is NOT NULL;
+ ----------------- + ---------------- +
| tutorial_author | tutorial_count |
+ ----------------- + ---------------- +
| 马赫兰 20 |
| 鳃| 20 |
+ ----------------- + ---------------- +
2行(0.00秒)
登入後複製

使用PHP腳本處理NULL值

PHP腳本中你可以在if ... else語句來處理變數是否為空,並產生對應的條件語句。

以下實例中PHP設定了$ tutorial_count變量,然後使用該變數與資料表中的tutorial_count欄位進行比較:

<?PHP
$ dbhost =&#39;localhost:3036&#39;;
$ dbuser =&#39;root&#39;;
$ dbpass =&#39;rootpassword&#39;;
$ conn = mysql_connect($ dbhost,$ dbuser,$ dbpass);
if(!$ conn)
{
  die(&#39;无法连接:&#39;。mysql_error());
}
if(isset($ tutorial_count))
{
   $ sql =&#39;SELECT tutorial_author,tutorial_count
           FROM tcount_tbl
           WHERE tutorial_count = $ tutorial_count&#39;;
}
其他
{
   $ sql =&#39;SELECT tutorial_author,tutorial_count
           FROM tcount_tbl
           WHERE tutorial_count IS $ tutorial_count&#39;;
}
mysql_select_db( &#39;教程&#39;);
$ retval = mysql_query($ sql,$ conn);
如果(!$ retval)
{
  die(&#39;无法获取数据:&#39;mysql_error());
}
while($ row = mysql_fetch_array($ retval,MYSQL_ASSOC))
{
    echo“作者:{$ row [&#39;tutorial_author&#39;]} <br>”。
         “Count:{$ row [&#39;tutorial_count&#39;]} <br>”。
         “--------------------------------结果”;
} 
echo“成功获取数据\ n”;
mysql_close($康恩);
?>
登入後複製

【相關推薦】

# 1. 特別推薦「php程式設計師工具箱」V0.1版本下載

2. 免費mysql線上視頻教學

3. #資料庫設計那些事

#

以上是MySQL NULL 值處理實例教學的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!