飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

处理 MySQL NULL 值

时间:2021-12-04  作者:匿名  

我们已经看到 SQL SELECT命令和WHERE子句从 MySQL 表中获取数据,但是当我们尝试给出一个条件,将字段或列值与NULL进行比较时,它无法正常工作。

为了处理这种情况,MySQL 提供了三个运算符 :

  • IS NULL - 如果列值为 NULL,则此运算符返回 true。

  • IS NOT NULL - 如果列值不为 NULL,则此运算符返回 true。

  • <=> - 此运算符比较值,即使对于两个 NULL 值也是如此(与 = 运算符不同)。

涉及 NULL 的条件是特殊的。您不能使用 = NULL或 != NULL在列中查找 NULL 值。这种比较总是失败,因为无法判断它们是否正确。有时,甚至 NULL = NULL 也会失败。

要查找是否为 NULL 的列,请使用IS NULL或IS NOT NULL。

在命令提示符下使用 NULL 值

假设TUTORIALS 数据库中有一个名为tcount_tbl的表,它包含两列,即tutorial_author和tutorial_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.
标签:MySQLNULL
搜你所爱
MySQL教程:目录
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。