要排序,请使用ORDER BY SUBSTRING()
。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Value varchar(40) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2321/78/54-6') -> ; mysql> insert into DemoTable values('2321/78/54-8'); mysql> insert into DemoTable values('2321/78/54-5'); mysql> insert into DemoTable values('2321/78/54-9');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+--------------+ | Value | +--------------+ | 2321/78/54-6 | | 2321/78/54-8 | | 2321/78/54-5 | | 2321/78/54-9 | +--------------+ 4 rows in set (0.00 sec)
这是对带有特殊字符的记录进行排序的查询-
mysql> select * from DemoTable -> where Value like '2321/78/54%' -> order by substring(Value from 1 for 10),cast(substring(Value from 11) as decimal);
这将产生以下输出-
+--------------+ | Value | +--------------+ | 2321/78/54-9 | | 2321/78/54-8 | | 2321/78/54-6 | | 2321/78/54-5 | +--------------+ 4 rows in set (0.05 sec)