为此,将FIELD()
方法与DESC一起使用。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Number int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(10); mysql> insert into DemoTable values(20); mysql> insert into DemoTable values(30); mysql> insert into DemoTable values(40); mysql> insert into DemoTable values(50);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
这将产生以下输出-
+--------+ | Number | +--------+ | 10 | | 20 | | 30 | | 40 | | 50 | +--------+ 5 rows in set (0.00 sec)
以下是在MySQL中放置参数后按降序对列进行排序的查询IN()
-
mysql> select *from DemoTable where Number IN (40,50,20,30,10) ORDER BY FIELD(Number, 40,50,20,30,10) DESC;
输出结果
这将产生以下输出-
+--------+ | Number | +--------+ | 10 | | 30 | | 20 | | 50 | | 40 | +--------+ 5 rows in set (0.03 sec)