要对字符串号进行排序,请使用CAST()
MySQL中的函数。语法如下-
SELECT *FROM yourTableName ORDER BY (yourColumnName as Decimal(integerValue,integerValueAfterDecimalPoint)) desc;
为了理解上述语法,让我们首先创建一个表。创建表的查询如下-
mysql> create table SortingStringDemo -> ( -> Amount varchar(10) -> );
使用insert命令在表中插入一些记录。查询如下。
mysql> insert into SortingStringDemo values('12.34'); mysql> insert into SortingStringDemo values('124.50'); mysql> insert into SortingStringDemo values('9.59'); mysql> insert into SortingStringDemo values('150.68'); mysql> insert into SortingStringDemo values('600.54');
使用select语句显示表中的所有记录。查询如下。
mysql> select *from SortingStringDemo;
以下是输出。
+--------+ | Amount | +--------+ | 12.34 | | 124.50 | | 9.59 | | 150.68 | | 600.54 | +--------+ 5 rows in set (0.00 sec)
这是按降序对字符串编号进行排序的查询。
mysql> select *from SortingStringDemo order by cast(Amount as Decimal(6,2)) desc;
以下是输出。
+--------+ | Amount | +--------+ | 600.54 | | 150.68 | | 124.50 | | 12.34 | | 9.59 | +--------+ 5 rows in set (0.00 sec)