首先按升序查詢一行中的非空值,然後顯示 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 次瀏覽

開啟您的事業

完成課程即可取得認證

立即開始
廣告