首先在MySQL中指定字段值ORDER BY
要先在MySQL中按特定字段值排序,请使用ORDERBYFIELD()
。让我们首先创建一个表-
mysql> create table DemoTable849(Color varchar(100));
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable849 values('RED'); mysql> insert into DemoTable849 values('ORANGE'); mysql> insert into DemoTable849 values('BLUE'); mysql> insert into DemoTable849 values('GREEN');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable849;
这将产生以下输出-
+--------+ | Color | +--------+ | RED | | ORANGE | | BLUE | | GREEN | +--------+ 4 rows in set (0.00 sec)
以下是先按特定值排序的查询-
mysql> select *from DemoTable849 order by field(Color,'RED','GREEN','BLUE','ORANGE');
这将产生以下输出-
+--------+ | Color | +--------+ | RED | | GREEN | | BLUE | | ORANGE | +--------+ 4 rows in set (0.00 sec)