在MySQL中高级排序以显示以J开头的字符串,即使使用ORDER BY排列后也是在末尾
为此,使用ORDER BY LIKE运算符。让我们首先创建一个表 –
mysql> create table DemoTable
(
Name varchar(100)
);
Query OK, 0 rows affected (0.60 sec)
使用插入命令在表中插入一些记录 –
mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Adam');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('Jace');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('Sam');
Query OK, 1 row affected (0.20 sec)
使用select语句从表中显示所有记录 –
mysql> select *from DemoTable;
这将产生以下输出 –
+-------+
| Name |
+-------+
| John |
| Adam |
| Chris |
| Jace |
| David |
| Sam |
+-------+
6 rows in set (0.00 sec)
以下是显示以J开头的字符串,在ORDER BY排列后即使在末尾的查询 –
mysql> select *from DemoTable order by Name like '%J%',Name;
这将产生以下输出 –
+-------+
| Name |
+-------+
| Adam |
| Chris |
| David |
| Sam |
| Jace |
| John |
+-------+
6 rows in set (0.00 sec)
阅读更多:MySQL 教程