阅读(4854) (12)

MySQL NULL 值处理

2015-09-12 06:20:52 更新

我们已经知道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 值

以下实例中假设数据库 W3CSCHOOL 中的表 tcount_tbl 含有两列 zijiebao_author 和 zijiebao_count, zijiebao_count 中设置插入NULL值。

实例

尝试以下实例:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use W3CSCHOOL;
Database changed
mysql> create table tcount_tbl
    -> (
    -> zijiebao_author varchar(40) NOT NULL,
    -> zijiebao_count  INT
    -> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO tcount_tbl
    -> (zijiebao_author, zijiebao_count) values ('mahran', 20);
mysql> INSERT INTO tcount_tbl
    -> (zijiebao_author, zijiebao_count) values ('mahnaz', NULL);
mysql> INSERT INTO tcount_tbl
    -> (zijiebao_author, zijiebao_count) values ('Jen', NULL);
mysql> INSERT INTO tcount_tbl
    -> (zijiebao_author, zijiebao_count) values ('Gill', 20);

mysql> SELECT * from tcount_tbl;
+-----------------+----------------+
| zijiebao_author | zijiebao_count |
+-----------------+----------------+
| mahran          |             20 |
| mahnaz          |           NULL |
| Jen             |           NULL |
| Gill            |             20 |
+-----------------+----------------+
4 rows in set (0.00 sec)

mysql>

以下实例中你可以看到 = 和 != 运算符是不起作用的:

mysql> SELECT * FROM tcount_tbl WHERE zijiebao_count = NULL;
Empty set (0.00 sec)
mysql> SELECT * FROM tcount_tbl WHERE zijiebao_count != NULL;
Empty set (0.01 sec)

查找数据表中 zijiebao_count 列是否为 NULL,必须使用IS NULL和IS NOT NULL,如下实例:

mysql> SELECT * FROM tcount_tbl 
    -> WHERE zijiebao_count IS NULL;
+-----------------+----------------+
| zijiebao_author | zijiebao_count |
+-----------------+----------------+
| mahnaz          |           NULL |
| Jen             |           NULL |
+-----------------+----------------+
2 rows in set (0.00 sec)
mysql> SELECT * from tcount_tbl 
    -> WHERE zijiebao_count IS NOT NULL;
+-----------------+----------------+
| zijiebao_author | zijiebao_count |
+-----------------+----------------+
| mahran          |             20 |
| Gill            |             20 |
+-----------------+----------------+
2 rows in set (0.00 sec)

使用PHP脚本处理 NULL 值

PHP脚本中你可以在 if...else 语句来处理变量是否为空,并生成相应的条件语句。

以下实例中PHP设置了$zijiebao_count变量,然后使用该变量与数据表中的 zijiebao_count 字段进行比较:

关于其中php的语法可以见于https://www.zijiebao.com/php

isset()   https://www.zijiebao.com/php/php-isset.html 

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
if( isset($zijiebao_count ))
{
   $sql = 'SELECT zijiebao_author, zijiebao_count
           FROM  tcount_tbl
           WHERE zijiebao_count = $zijiebao_count';
}
else
{
   $sql = 'SELECT zijiebao_author, zijiebao_count
           FROM  tcount_tbl
           WHERE zijiebao_count IS $zijiebao_count';
}

mysql_select_db('W3CSCHOOL');
$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['zijiebao_author']}  <br> ".
         "Count: {$row['zijiebao_count']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfullyn";
mysql_close($conn);
?>