MySQLi - 處理 NULL 值



我們已經瞭解了 SQL 的 SELECT 命令以及 WHERE 子句,用於從 MySQL 表中獲取資料,但是當我們嘗試給出比較欄位或列值與 NULL 的條件時,它無法正常工作。

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

  • IS NULL - 如果列值為 NULL,則此運算子返回 true。

  • IS NOT NULL - 如果列值不為 NULL,則此運算子返回 true。

  • <=> - 此運算子比較值,即使對於兩個 NULL 值也為 true(與 = 運算子不同)。

涉及 NULL 的條件是特殊的。您不能使用 = NULL 或 != NULL 來查詢列中的 NULL 值。此類比較始終失敗,因為無法判斷它們是真還是假。有時,即使 NULL = NULL 也會失敗。

要查詢是或不是 NULL 的列,請使用 IS NULLIS NOT NULL

在命令提示符下使用 NULL 值

假設在 TUTORIALS 資料庫中有一個名為 tcount_tbl 的表,它包含兩個列,即 tutorial_authortutorial_count,其中 NULL 的 tutorial_count 表示該值未知。

示例

嘗試以下示例:

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>

您可以看到 = 和 != 不適用於 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 的記錄,查詢應按以下程式中所示編寫。

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 值

您可以使用 if...else 條件根據 NULL 值準備查詢。

以下示例從外部獲取 tutorial_count,然後將其與表中可用的值進行比較。

示例

複製並貼上以下示例作為 mysql_example.php:

<html>
   <head>
      <title>Handling NULL</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = 'root@123';
         $dbname = 'TUTORIALS';
         $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
         $tutorial_count = null;
         if($mysqli→connect_errno ) {
            printf("Connect failed: %s<br />", $mysqli→connect_error);
            exit();
         }
         printf('Connected successfully.<br />');
   
         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 NULL';
         }
         $result = $mysqli→query($sql);
           
         if ($result→num_rows > 0) {
            while($row = $result→fetch_assoc()) {
               printf("Author: %s, Count: %d <br />",
                  $row["tutorial_author"], 
                  $row["tutorial_count"]);               
            }
         } else {
            printf('No record found.<br />');
         }
         $mysqli→close();
      ?>
   </body>
</html>

輸出

訪問部署在 apache web 伺服器上的 mysql_example.php 並驗證輸出。

Connected successfully.
No record found.
廣告