To sort from max to min value, use ORDER BY length(). Let us first create a table −
mysql> create table DemoTable -> ( -> Price varchar(20) -> ); Query OK, 0 rows affected (0.92 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('80');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('800');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('108');
Query OK, 1 row affected (0.50 sec)
mysql> insert into DemoTable values('765');
Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Price | +-------+ | 80 | | 800 | | 108 | | 765 | +-------+ 4 rows in set (0.00 sec)
Here is the query to sort max to min value in MySQL −
mysql> select *from DemoTable -> order by length(Price) DESC,Price DESC;
This will produce the following output −
+-------+ | Price | +-------+ | 800 | | 765 | | 108 | | 80 | +-------+ 4 rows in set (0.00 sec)