- MySQLi 教程
- MySQLi - 首页
- MySQLi - 简介
- MySQLi - PHP 语法
- MySQLi - 连接
- MySQLi - 创建数据库
- MySQLi - 删除数据库
- MySQLi - 选择数据库
- MySQLi - 创建表
- MySQLi - 删除表
- MySQLi - 插入查询
- MySQLi - 选择查询
- MySQLi - WHERE 子句
- MySQLi - 更新查询
- MySQLi - 删除查询
- MySQLi - LIKE 子句
- MySQLi - 排序结果
- MySQLi - 使用连接
- MySQLi - 处理 NULL 值
- 获取和使用 MySQLi 元数据
- MySQL
- MySQL - 安装
- MySQL - 管理
- MySQL - 数据类型
- MySQL - 正则表达式
- MySQL - 事务
- MySQL - ALTER 命令
- MySQL - 索引
- MySQL - 临时表
- MySQL - 克隆表
- MySQL - 使用序列
- MySQL - 处理重复数据
- MySQLi 有用资源
- MySQLi - 有用函数
- MySQLi - 快速指南
- MySQLi - 有用资源
- MySQLi - 讨论
MySQLi - 处理 NULL 值
我们已经了解了 SQL 的 SELECT 命令以及 WHERE 子句,用于从 MySQL 表中获取数据,但是当我们尝试给出比较字段或列值与 NULL 的条件时,它无法正常工作。
为了处理这种情况,MySQL 提供了三个运算符:
IS NULL - 如果列值为 NULL,则此运算符返回 true。
IS NOT NULL - 如果列值不为 NULL,则此运算符返回 true。
<=> - 此运算符比较值,即使对于两个 NULL 值也为 true(与 = 运算符不同)。
涉及 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.
广告