您可以为此使用CASE语句。让我们首先创建一个表-
mysql> create table DemoTable ( Number int );
使用插入命令在表中插入记录-
mysql> insert into DemoTable values(490); mysql> insert into DemoTable values(310); mysql> insert into DemoTable values(540); mysql> insert into DemoTable values(123); mysql> insert into DemoTable values(1230); mysql> insert into DemoTable values(1090);
使用select语句显示表中的所有记录。查询如下-
mysql> select * from DemoTable;
这将产生以下输出-
+--------+ | Number | +--------+ | 490 | | 310 | | 540 | | 123 | | 1230 | | 1090 | +--------+ 6 rows in set (0.00 sec)
以下是MySQL中按选定记录排序的查询-
mysql> select *from DemoTable order by (case when Number = 1090 then 0 else 1 end), Number;
这将产生以下输出-
+--------+ | Number | +--------+ | 1090 | | 123 | | 310 | | 490 | | 540 | | 1230 | +--------+ 6 rows in set (0.00 sec)