在 MySQL 中显示 NULL 和 NOT NULL 记录,但排除某个具体的值
要显示 NULL 记录,在 MySQL 中使用 IS NULL。要忽略单个值,请使用 != 运算符,它别名是 <> 运算符。
我们首先创建一个表 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, PlayerName varchar(40) ); Query OK, 0 rows affected (0.50 sec)
使用 insert 命令向表中插入一些记录 ->
mysql> insert into DemoTable(PlayerName) values('Adam');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(PlayerName) values(NULL);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(PlayerName) values('Sam');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(PlayerName) values('Mike');
Query OK, 1 row affected (0.08 sec)使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
这将生成以下输出 -
+----+------------+ | Id | PlayerName | +----+------------+ | 1 | Adam | | 2 | NULL | | 3 | Sam | | 4 | Mike | +----+------------+ 4 rows in set (0.00 sec)
以下是显示 NULL 和 NOT NULL 记录的查询,忽略单个特定记录 -
mysql> select *from DemoTable where PlayerName!='Sam' or PlayerName IS NULL;
这将生成以下输出 -
+----+------------+ | Id | PlayerName | +----+------------+ | 1 | Adam | | 2 | NULL | | 4 | Mike | +----+------------+ 3 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP