为了先按0然后最大的顺序排序,可以使用以下语法-
select *from yourTableName order by yourColumnName=0 DESC,yourColumnName DESC;
让我们首先创建一个表-
mysql> create table DemoTable ( Value int );
使用插入命令在表中插入记录-
mysql> insert into DemoTable values(90); mysql> insert into DemoTable values(0); mysql> insert into DemoTable values(20); mysql> insert into DemoTable values(0); mysql> insert into DemoTable values(10); mysql> insert into DemoTable values(70); mysql> insert into DemoTable values(0);
使用选择命令显示表中的记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------+ | Value | +-------+ | 90 | | 0 | | 20 | | 0 | | 10 | | 70 | | 0 | +-------+ 7 rows in set (0.00 sec)
这是按0排序的查询,然后按DESC顺序显示最大元素-
mysql> select *from DemoTable order by Value=0 DESC,Value DESC;
这将产生以下输出-
+-------+ | Value | +-------+ | 0 | | 0 | | 0 | | 90 | | 70 | | 20 | | 10 | +-------+ 7 rows in set (0.00 sec)