在 MySQL 中使用高级排序,以便在 ORDER BY 之后仍将在 J 开头的字符串显示在最后面
为此,请使用 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)
以下是即使在 ORDER BY 之后仍将以 J 开头的字符串显示在最后面的查询 -
mysql> select *from DemoTable order by Name like '%J%',Name;
这将产生以下输出 -
+-------+ | Name | +-------+ | Adam | | Chris | | David | | Sam | | Jace | | John | +-------+ 6 rows in set (0.00 sec)
广告