是的,您可以使用FIELD()
MySQL的ORDER BY来实现。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Number int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(19); mysql> insert into DemoTable values(30); mysql> insert into DemoTable values(34); mysql> insert into DemoTable values(28); mysql> insert into DemoTable values(25); mysql> insert into DemoTable values(29); mysql> insert into DemoTable values(24);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------+ | Number | +--------+ | 19 | | 30 | | 34 | | 28 | | 25 | | 29 | | 24 | +--------+ 7 rows in set (0.00 sec)
以下是以与设置的值相同的顺序返回结果的查询-
mysql> select *from DemoTable -> ORDER BY FIELD(Number, 30,19,34,25,28,29,24) ;
这将产生以下输出,以相同顺序显示结果-
+--------+ | Number | +--------+ | 30 | | 19 | | 34 | | 25 | | 28 | | 29 | | 24 | +--------+ 7 rows in set (0.00 sec)