MySQL查询对同一字段执行排序顺序
为此,请使用ORDERBYIF()
。
让我们首先创建一个表-
mysql> create table DemoTable801 ( Score int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable801 values(30); mysql> insert into DemoTable801 values(99); mysql> insert into DemoTable801 values(45); mysql> insert into DemoTable801 values(55); mysql> insert into DemoTable801 values(99); mysql> insert into DemoTable801 values(69); mysql> insert into DemoTable801 values(25); mysql> insert into DemoTable801 values(79);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable801;
这将产生以下输出-
+-------+ | Score | +-------+ | 30 | | 99 | | 45 | | 55 | | 99 | | 69 | | 25 | | 79 | +-------+ 8 rows in set (0.00 sec)
以下是对同一字段执行排序顺序的查询-
mysql> select *from DemoTable801 order by if(Score=99,1,0), Score;
这将产生以下输出-
+-------+ | Score | +-------+ | 25 | | 30 | | 45 | | 55 | | 69 | | 79 | | 99 | | 99 | +-------+ 8 rows in set (0.00 sec)