MySQL NULL 值处理

我们已经知道 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 运算符。

注意:

select * , columnName1+ifnull(columnName2,0) from tableName;

columnName1,columnName2 为 int 型,当 columnName2 中,有值为 null 时,columnName1+columnName2=null, ifnull(columnName2,0) 把 columnName2 中 null 值转为 0。


在命令提示符中使用 NULL 值

以下实例中假设数据库 RUNOOB 中的表 yht7_test_tbl 含有两列 yht7_author 和 yht7_count, yht7_count 中设置插入NULL值。

实例

尝试以下实例:

创建数据表 yht7_test_tbl

root@host# mysql -u root -p password; Enter password:******* mysql> use RUNOOB; Database changed mysql> create table yht7_test_tbl -> ( -> yht7_author varchar(40) NOT NULL, -> yht7_count INT -> ); Query OK, 0 rows affected (0.05 sec) mysql> INSERT INTO yht7_test_tbl (yht7_author, yht7_count) values ("RUNOOB", 20); mysql> INSERT INTO yht7_test_tbl (yht7_author, yht7_count) values ("天天编程", NULL); mysql> INSERT INTO yht7_test_tbl (yht7_author, yht7_count) values ("Google", NULL); mysql> INSERT INTO yht7_test_tbl (yht7_author, yht7_count) values ("FK", 20); mysql> SELECT * from yht7_test_tbl; +---------------+--------------+ | yht7_author | yht7_count | +---------------+--------------+ | RUNOOB | 20 | | 天天编程 | NULL | | Google | NULL | | FK | 20 | +---------------+--------------+ 4 rows in set (0.01 sec)

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

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

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

mysql> SELECT * FROM yht7_test_tbl WHERE yht7_count IS NULL; +---------------+--------------+ | yht7_author | yht7_count | +---------------+--------------+ | 天天编程 | NULL | | Google | NULL | +---------------+--------------+ 2 rows in set (0.01 sec) mysql> SELECT * from yht7_test_tbl WHERE yht7_count IS NOT NULL; +---------------+--------------+ | yht7_author | yht7_count | +---------------+--------------+ | RUNOOB | 20 | | FK | 20 | +---------------+--------------+ 2 rows in set (0.01 sec)

使用 PHP 脚本处理 NULL 值

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

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

MySQL ORDER BY 测试:

<?php $dbhost = "localhost:3306"; // mysql服务器主机地址 $dbuser = "root"; // mysql用户名 $dbpass = "123456"; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die("连接失败: " . mysqli_error($conn)); } // 设置编码,防止中文乱码 mysqli_query($conn , "set names utf8"); if( isset($yht7_count )) { $sql = "SELECT yht7_author, yht7_count FROM yht7_test_tbl WHERE yht7_count = $yht7_count"; } else { $sql = "SELECT yht7_author, yht7_count FROM yht7_test_tbl WHERE yht7_count IS NULL"; } mysqli_select_db( $conn, "RUNOOB" ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die("无法读取数据: " . mysqli_error($conn)); } echo "<h2>天天编程 IS NULL 测试<h2>"; echo "<table border="1"><tr><td>作者</td><td>登陆次数</td></tr>"; while($row = mysqli_fetch_array($retval, MYSQL_ASSOC)) { echo "<tr>". "<td>{$row["yht7_author"]} </td> ". "<td>{$row["yht7_count"]} </td> ". "</tr>"; } echo "</table>"; mysqli_close($conn); ?>

输出结果如下图所示: