我们可以在 MySQL 中使用 ORDER BY NULL 吗?

mysqlmysqli database更新于 2025/5/26 12:28:17

是的,我们可以这样做

注意 − 在 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)


相关文章


有用资源