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)永远返回 NULL,即 NULL = NULL 返回 NULL 。
MySQL 中处理 NULL 使用 IS NULL 和 IS NOT NULL 运算符。
注意:
select*,columnName1+ifnull(columnName2,0)fromtableName;
columnName1,columnName2 为 int 型,当 columnName2 中,有值为 null 时,columnName1+columnName2=null, ifnull(columnName2,0) 把 columnName2 中 null 值转为 0。
在命令提示符中使用 NULL 值
以下实例中假设数据库 GTXYZZ 中的表 gtxyzz_test_tbl 含有两列 gtxyzz_author 和 gtxyzz_count, gtxyzz_count 中设置插入NULL值。
实例
尝试以下实例:
创建数据表 gtxyzz_test_tbl
root@host#mysql-uroot-ppassword; Enterpassword:******* mysql>useGTXYZZ; Databasechanged mysql>createtablegtxyzz_test_tbl( ->gtxyzz_authorvarchar(40)NOTNULL, ->gtxyzz_countINT); QueryOK,0rowsaffected(0.05sec) mysql>INSERTINTOgtxyzz_test_tbl(gtxyzz_author,gtxyzz_count)values('GTXYZZ',20); mysql>INSERTINTOgtxyzz_test_tbl(gtxyzz_author,gtxyzz_count)values('1818IP教程',NULL); mysql>INSERTINTOgtxyzz_test_tbl(gtxyzz_author,gtxyzz_count)values('Google',NULL); mysql>INSERTINTOgtxyzz_test_tbl(gtxyzz_author,gtxyzz_count)values('FK',20); mysql>SELECT*fromgtxyzz_test_tbl; +---------------+--------------+ |gtxyzz_author|gtxyzz_count| +---------------+--------------+ |GTXYZZ|20| |1818IP教程|NULL| |Google|NULL| |FK|20| +---------------+--------------+ 4rowsinset(0.00sec)
以下实例中你可以看到 = 和 != 运算符是不起作用的:
mysql>SELECT*FROMgtxyzz_test_tblWHEREgtxyzz_count=NULL; Emptyset(0.00sec) mysql>SELECT*FROMgtxyzz_test_tblWHEREgtxyzz_count!=NULL; Emptyset(0.01sec)
查找数据表中 gtxyzz_test_tbl 列是否为 NULL,必须使用 IS NULL 和 IS NOT NULL,如下实例:
mysql>SELECT*FROMgtxyzz_test_tblWHEREgtxyzz_countISNULL; +---------------+--------------+ |gtxyzz_author|gtxyzz_count| +---------------+--------------+ |1818IP教程|NULL| |Google|NULL| +---------------+--------------+ 2rowsinset(0.00sec) mysql>SELECT*fromgtxyzz_test_tblWHEREgtxyzz_countISNOTNULL; +---------------+--------------+ |gtxyzz_author|gtxyzz_count| +---------------+--------------+ |GTXYZZ|20| |FK|20| +---------------+--------------+ 2rowsinset(0.01sec)
使用 PHP 脚本处理 NULL 值
PHP 脚本中你可以在 if…else 语句来处理变量是否为空,并生成相应的条件语句。
以下实例中 PHP 设置了 $gtxyzz_count 变量,然后使用该变量与数据表中的 gtxyzz_count 字段进行比较:
MySQL ORDER BY 测试:
<?php $dbhost='localhost';//mysql服务器主机地址 $dbuser='root';//mysql用户名 $dbpass='123456';//mysql用户名密码 $conn=mysqli_connect($dbhost,$dbuser,$dbpass); if(!$conn) { die('连接失败:'.mysqli_error($conn)); } //设置编码,防止中文乱码 mysqli_query($conn,"setnamesutf8"); if(isset($gtxyzz_count)) { $sql="SELECTgtxyzz_author,gtxyzz_count FROMgtxyzz_test_tbl WHEREgtxyzz_count=$gtxyzz_count"; } else { $sql="SELECTgtxyzz_author,gtxyzz_count FROMgtxyzz_test_tbl WHEREgtxyzz_countISNULL"; } mysqli_select_db($conn,'GTXYZZ'); $retval=mysqli_query($conn,$sql); if(!$retval) { die('无法读取数据:'.mysqli_error($conn)); } echo'<h2>1818IP教程ISNULL测试<h2>'; echo'<tableborder="1"><tr><td>作者</td><td>登陆次数</td></tr>'; while($row=mysqli_fetch_array($retval,MYSQL_ASSOC)) { echo"<tr>". "<td>{$row['gtxyzz_author']}</td>". "<td>{$row['gtxyzz_count']}</td>". "</tr>"; } echo'</table>'; mysqli_close($conn); ?>