如何在MySQL中按特定字符串排序?
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> FirstName varchar(100) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('John'); mysql> insert into DemoTable values('Adam'); mysql> insert into DemoTable values('Sam'); mysql> insert into DemoTable values('Johnny'); mysql> insert into DemoTable values('Joy'); mysql> insert into DemoTable values('Jace');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
这将产生以下输出-
+-----------+ | FirstName | +-----------+ | John | | Adam | | Sam | | Johnny | | Joy | | Jace | +-----------+ 6 rows in set (0.00 sec)
以下是按特定字符串排序的查询。假设您需要通过子字符串'Jo'进行一阶操作-
mysql> select *from DemoTable -> order by case when substring(FirstName, 1, 2) = 'Jo' then 0 else 1 end;
输出结果
这将产生以下输出-
+-----------+ | FirstName | +-----------+ | John | | Johnny | | Joy | | Adam | | Sam | | Jace | +-----------+ 6 rows in set (0.00 sec)