使用 MySQL 仅按 ASC 顺序显示记录列表
mysqlmysqli database更新于 2025/9/29 13:00:17
要按特定顺序显示记录列表,您需要设置条件并使用 ORDER BY。为此,请使用 ORDER BY CASE 语句。让我们首先创建一个表 −
mysql> create table DemoTable2039 -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.62 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable2039 values('John Doe');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable2039 values('John Smith');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable2039 values('Chris Brown');
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable2039 values('Adam Smith');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable2039 values('David Miller');
Query OK, 1 row affected (0.09 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable2039;
这将产生以下输出 −
+--------------+ | Name | +--------------+ | John Doe | | John Smith | | Chris Brown | | Adam Smith | | David Miller | +--------------+ 5 rows in set (0.00 sec)
以下查询按 ASC 顺序显示特定的记录列表 −
mysql> select *from DemoTable2039 -> order by -> case when Name like '%Smith%' then 101 -> else -> 100 -> end, -> Name;
这将产生以下输出 −
+--------------+ | Name | +--------------+ | Chris Brown | | David Miller | | John Doe | | Adam Smith | | John Smith | +--------------+ 5 rows in set (0.37 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

