我們可以在 MySQL 中使用 ORDER BY NULL 嗎?


是的,我們可以這樣做

注意 − 在 MySQL 5.7 之前,ORDER BY NULL 很有用,但使用 MySQL 8.0 時,指定 ORDER BY NULL(例如,在末尾)以抑制隱式排序不再有必要。

我們先建立一個表格 −

mysql> create table DemoTable
-> (
-> Name varchar(10)
-> );
Query OK, 0 rows affected (1.01 sec)

使用 insert 命令在表中插入一些記錄 −

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable values('Bob');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values('Sam');
Query OK, 1 row affected (0.08 sec)

mysql> insert into DemoTable values('Mike');
Query OK, 1 row affected (0.13 sec)

使用 select 語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

輸出

將生成以下輸出 −

+-------+
| Name  |
+-------+
| John  |
| Bob   |
| David |
| Sam   |
| Mike  |
+-------+
5 rows in set (0.00 sec)

以下是如何在 MySQL 中使用 ORDER BY NULL −

mysql> select *from DemoTable order by NULL;

輸出

將生成以下輸出 −

+-------+
| Name  |
+-------+
| John  |
| Bob   | 
| David |
| Sam   |
| Mike  |
+-------+
5 rows in set (0.00 sec)

更新於:30-6 月-2020

369 次觀看

啟動你的 職業生涯

完成課程,獲取認證

開始
廣告