MySQL从最高值到最低值排序?
要按从最高到最低的顺序排序,可以使用ORDERBYDESC命令-
select *from yourTableName order by yourColumnName DESC;
如果您希望结果从最低到最高,可以使用ORDERBYASC命令-
select *from yourTableName order by yourColumnName ASC;
让我们首先创建一个表-
mysql> create table DemoTable ( Value int );
使用插入命令在表中插入记录-
mysql> insert into DemoTable values(134); mysql> insert into DemoTable values(245); mysql> insert into DemoTable values(451); mysql> insert into DemoTable values(1090); mysql> insert into DemoTable values(789); mysql> insert into DemoTable values(434);
使用选择命令显示表中的记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------+ | Value | +-------+ | 134 | | 245 | | 451 | | 1090 | | 789 | | 434 | +-------+ 6 rows in set (0.00 sec)
情况1-按从最高到最低的顺序排列-
mysql> select *from DemoTable order by Value DESC;
这将产生以下输出-
+-------+ | Value | +-------+ | 1090 | | 789 | | 451 | | 434 | | 245 | | 134 | +-------+ 6 rows in set (0.07 sec)
情况2-按从最低到最高的顺序排列-
mysql> select *from DemoTable order by Value ASC;
这将产生以下输出-
+-------+ | Value | +-------+ | 134 | | 245 | | 434 | | 451 | | 789 | | 1090 | +-------+ 6 rows in set (0.00 sec)