首先显示值非 Null,然后在 MySQL 中显示值 Null 的结果
我们首先创建一个表 -
mysql> create table DemoTable1357 -> ( -> StudentName varchar(40), -> StudentCountryName varchar(30) -> ); Query OK, 0 rows affected (0.49 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1357 values('Chris','US'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1357 values('David',NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1357 values('David','AUS'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1357 values('Carol',NULL); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1357 values('Mike','UK'); Query OK, 1 row affected (0.15 sec)
使用 select 语句从表中显示所有记录 -
mysql> select * from DemoTable1357;
这将生成以下输出 -
+-------------+--------------------+ | StudentName | StudentCountryName | +-------------+--------------------+ | Chris | US | | David | NULL | | David | AUS | | Carol | NULL | | Mike | UK | +-------------+--------------------+ 5 rows in set (0.00 sec)
以下是显示结果,首先显示非 NULL 值,然后显示 NULL 值的查询 -
mysql> select * from DemoTable1357 -> order by (StudentCountryName IS NULL),StudentName;
这将生成以下输出 -
+-------------+--------------------+ | StudentName | StudentCountryName | +-------------+--------------------+ | Chris | US | | David | AUS | | Mike | UK | | Carol | NULL | | David | NULL | +-------------+--------------------+ 5 rows in set (0.00 sec)
广告