MySQL ORDER BY 含有下划线的字符串?
让我们首先创建一个表 -
mysql> create table DemoTable ( Name varchar(100) ); Query OK, 0 rows affected (0.60 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('John_Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('John_Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.13 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+--------------+ | Name | +--------------+ | John_Smith | | Chris Brown | | John_Doe | | David Miller | | Carol Taylor | +--------------+ 5 rows in set (0.00 sec)
以下是按带有下划线的字符串排序的查询 -
mysql> select *from DemoTable order by (Name LIKE 'John_%') DESC,rand();
这将产生以下输出 -
+--------------+ | Name | +--------------+ | John_Doe | | John_Smith | | Carol Taylor | | David Miller | | Chris Brown | +--------------+ 5 rows in set (0.42 sec)
广告