使用 MySQL 实现特定的记录排序
要设置特定的记录顺序,请使用 ORDER BY LIKE。让我们首先创建一个表 -
mysql> create table DemoTable808(Value varchar(100)); Query OK, 0 rows affected (0.61 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable808 values('smith');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable808 values('Adamsmith');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable808 values('Carolsmith');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable808 values('smithJohn');
Query OK, 1 row affected (0.16 sec)使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable808;
这将产生以下输出 -
+------------+ | Value | +------------+ | smith | | Adamsmith | | Carolsmith | | smithJohn | +------------+ 4 rows in set (0.00 sec)
以下是设置特定顺序的查询 -
mysql> select *from DemoTable808 order by case when Value like 'smith%' then 0 else 1 end asc,Value asc;
这将产生以下输出 -
+------------+ | Value | +------------+ | smith | | smithJohn | | Adamsmith | | Carolsmith | +------------+ 4 rows in set (0.00 sec)
广告
Data 结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP