MySQL 处理NULL值
我们已经了解了使用SQL的 SELECT 命令和 WHERE 子句从MySQL表中获取数据,但是当我们尝试给出一个条件来比较字段或列值与 NULL 时,它无法正常工作。
为了处理这种情况,MySQL提供了三个操作符-
- IS NULL - 如果列值为NULL,则返回true。
-
IS NOT NULL - 如果列值不为NULL,则返回true。
-
<=>
- 该操作符比较值,与=
操作符不同的是,即使是两个NULL值也返回true。
涉及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或不为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.