在MySQL中实现自定义排序顺序
要在MySQL中实现自定义排序顺序,您需要使用ORDERBYFIELD()。让我们首先创建一个表-
create table DemoTable -> ( -> Designation varchar(100) -> );
使用插入命令在表中插入一些记录-
insert into DemoTable values('Software Engineer'); insert into DemoTable values('Associate Software Engineer'); insert into DemoTable values('Software Development Engineer'); insert into DemoTable values('Product Manager');
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+-------------------------------+ | Designation | +-------------------------------+ | Software Engineer | | Associate Software Engineer | | Software Development Engineer | | Product Manager | +-------------------------------+ 4 rows in set (0.00 sec)
以下是在MySQL中实现自定义排序顺序的查询-
select *from DemoTable -> order by field(Designation,'Associate Software Engineer','Software Engineer','Software Development Engineer','Product Manager');
这将产生以下输出-
+-------------------------------+ | Designation | +-------------------------------+ | Associate Software Engineer | | Software Engineer | | Software Development Engineer | | Product Manager | +-------------------------------+ 4 rows in set (0.02 sec)