主頁(yè) > 知識(shí)庫(kù) > MySQL NULL 值處理實(shí)例詳解

MySQL NULL 值處理實(shí)例詳解

熱門(mén)標(biāo)簽:東莞外呼企業(yè)管理系統(tǒng) 沈陽(yáng)智能外呼系統(tǒng)供應(yīng)商 桂林云電銷(xiāo)機(jī)器人收費(fèi) 如何選擇優(yōu)質(zhì)的外呼系統(tǒng) 南通電銷(xiāo)外呼系統(tǒng)哪家強(qiáng) 谷歌地圖標(biāo)注位置圖解 地圖簡(jiǎn)圖標(biāo)注 清遠(yuǎn)申請(qǐng)400電話 手機(jī)外呼系統(tǒng)違法嗎

MySQL NULL 值處理

我們已經(jīng)知道MySQL使用 SQL SELECT 命令及 WHERE 子句來(lái)讀取數(shù)據(jù)表中的數(shù)據(jù),但是當(dāng)提供的查詢(xún)條件字段為 NULL 時(shí),該命令可能就無(wú)法正常工作。

為了處理這種情況,MySQL提供了三大運(yùn)算符:

  • IS NULL: 當(dāng)列的值是NULL,此運(yùn)算符返回true。
  • IS NOT NULL: 當(dāng)列的值不為NULL, 運(yùn)算符返回true。
  • =>: 比較操作符(不同于=運(yùn)算符),當(dāng)比較的的兩個(gè)值為NULL時(shí)返回true。

關(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)算符。

在命令提示符中使用 NULL 值

以下實(shí)例中假設(shè)數(shù)據(jù)庫(kù) TUTORIALS 中的表 tcount_tbl 含有兩列 tutorial_author 和 tutorial_count, tutorial_count 中設(shè)置插入NULL值。

實(shí)例

嘗試以下實(shí)例:

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>

以下實(shí)例中你可以看到 = 和 != 運(yùn)算符是不起作用的:

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)

查找數(shù)據(jù)表中 tutorial_count 列是否為 NULL,必須使用IS NULL和IS NOT NULL,如下實(shí)例:

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 語(yǔ)句來(lái)處理變量是否為空,并生成相應(yīng)的條件語(yǔ)句。

以下實(shí)例中PHP設(shè)置了$tutorial_count變量,然后使用該變量與數(shù)據(jù)表中的 tutorial_count 字段進(jìn)行比較:

?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);
?>

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

您可能感興趣的文章:
  • sqlserver 不能將值NULL插入列id(列不允許有空值解決)
  • mysql中將null值轉(zhuǎn)換為0的語(yǔ)句
  • MySQL中對(duì)于NULL值的理解和使用教程
  • 在SQL Server中使用ISNULL執(zhí)行空值判斷查詢(xún)
  • 詳解MySQL中的NULL值
  • sql 語(yǔ)句中的 NULL值
  • SQL 中 NULL值測(cè)試代碼
  • 在SQL中該如何處理NULL值

標(biāo)簽:常德 成都 內(nèi)蒙古 重慶 臨沂 天津 湖州 貴州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL NULL 值處理實(shí)例詳解》,本文關(guān)鍵詞  MySQL,NULL,值,處理,實(shí)例,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL NULL 值處理實(shí)例詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于MySQL NULL 值處理實(shí)例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章