在某些情况下,请使用ORDER BY。让我们创建一个表-mysql> create table demo18
−> (
−> value text
−> );
借助insert命令将一些记录插入表中-mysql> insert into demo18 values('John Smith');
mysql> insert into demo18 values('2J John has 58');
mysql> insert into demo18 values('2J John has 9');
使用select语句显示表中的记录-mysql> select *from demo18;
这将产生以下输出-+----------------+
| value |
+----------------+
| John Smith |
| 2J John has 58 |
| 2J John has 9 |
+----------------+
3 rows in set (0.00 sec)
以下是要排序的查询-mysql> select *from demo18
−> order by regexp_replace(value, '[0&minus9]*$', ''),
−> length(value),
−> value;
这将产生以下输出-+----------------+
| value |
+----------------+
| 2J John has 9 |
| 2J John has 58 |
| John Smith |
+----------------+
3 rows in set (0.14 sec)