首先按升序查询一行中的非空值,然后显示 NULL 值


对此,使用 ORDER BY ISNULL()。让我们先创建一个表格 −

mysql> create table DemoTable669
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentScore int
);
Query OK, 0 rows affected (0.55 sec)

使用 insert 命令将一些记录插入到表格中 −

mysql> insert into DemoTable669(StudentScore) values(45) ;
Query OK, 1 row affected (0.80 sec)
mysql> insert into DemoTable669(StudentScore) values(null);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable669(StudentScore) values(89);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable669(StudentScore) values(null);
Query OK, 1 row affected (0.15 sec)

使用 select 语句从表格中显示所有记录 −

mysql> select *from DemoTable669;

这会生成以下输出 −

+-----------+--------------+
| StudentId | StudentScore |
+-----------+--------------+
| 1         | 45           |
| 2         | NULL         |
| 3         | 89           |
| 4         | NULL         |
+-----------+--------------+
4 rows in set (0.00 sec)

以下是按升序显示非空值的查询。空值将在之后显示 −

mysql> select *from DemoTable669
ORDER BY ISNULL(StudentScore),StudentScore;

这会生成以下输出 −

+-----------+--------------+
| StudentId | StudentScore |
+-----------+--------------+
| 1         | 45           |  
| 3         | 89           |
| 2         | NULL         |
| 4         | NULL         |
+-----------+--------------+
4 rows in set (0.00 sec)

更新于: 21-Jan-2020

132 次浏览

开启您的事业

完成课程即可取得认证

立即开始
广告