MySQL 中是否有默认的 ORDER BY 值?
mysqlmysqli database更新于 2025/5/17 14:04:17
MySQL 中没有默认的 ORDER BY 值。您需要明确指定 ORDER BY 子句。以下是语法 −
ORDER BY ASC; OR ORDER BY DESC;
首先我们创建一个表 −
mysql> create table DemoTable -> ( -> StudentName varchar(100) -> ); Query OK, 0 rows affected (0.82 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('Sam');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Bob');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.25 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
这将产生以下输出 −
+-------------+ | StudentName | +-------------+ | Sam | | Chris | | David | | Bob | | Robert | +-------------+ 5 rows in set (0.00 sec)
案例 1 − 如果您希望结果按升序排列 −
mysql> select *from DemoTable order by StudentName asc;
输出
这将产生以下输出 −
+-------------+ | StudentName | +-------------+ | Bob | | Chris | | David | | Robert | | Sam | +-------------+ 5 rows in set (0.00 sec)
案例 2 − 如果您希望结果按降序排列 −
mysql> select *from DemoTable order by StudentName desc;
输出
这将产生以下输出 −
+-------------+ | StudentName | +-------------+ | Sam | | Robert | | David | | Chris | | Bob | +-------------+ 5 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

