MySQL 查询仅同时显示空值和 NULL 值?
mysqlmysqli database更新于 2025/10/21 18:52:17
要检查 NULL,请使用 IS NULL。对于空值,您需要使用空字符串进行检查。我们现在来看一个例子。
首先我们创建一个表 −
mysql> create table DemoTable691( PlayerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, PlayerName varchar(100), PlayerScore int ); Query OK, 0 rows affected (0.56 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable691(PlayerName,PlayerScore) values('Robert',56);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable691(PlayerName,PlayerScore) values('David',89);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable691(PlayerName,PlayerScore) values('',98);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable691(PlayerName,PlayerScore) values(null,71);
Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable691;
这将产生以下输出 −
+----------+------------+-------------+ | PlayerId | PlayerName | PlayerScore | +----------+------------+-------------+ | 1 | Robert | 56 | | 2 | David | 89 | | 3 | | 98 | | 4 | NULL | 71 | +----------+------------+-------------+ 4 rows in set (0.00 sec)
以下是 MySQL 查询,用于同时显示空值和 NULL 值 −
mysql> select *from DemoTable691 where PlayerName IS NULL OR PlayerName='';
这将产生以下输出 −
+----------+------------+-------------+ | PlayerId | PlayerName | PlayerScore | +----------+------------+-------------+ | 3 | | 98 | | 4 | NULL | 71 | +----------+------------+-------------+ 2 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

