数据库
首页 > 数据库> > mysql按自定义顺序排序

mysql按自定义顺序排序

作者:互联网

order by FIELD(排序列,value1,value2....) desc/asc

select * from sy_cd_ms_base_hospital_info order by FIELD(grade,"三甲","三乙","三丙","二甲") asc 

 

 图中按照"三甲","三乙","三丙","二甲"的顺序排列,没有在排序中包含的排在了上面

select * from sy_cd_ms_base_hospital_info order by FIELD(grade,"三甲","三乙","三丙","二甲") desc  

 

图中按照"三甲","三乙","三丙","二甲"降序排列,没有在排序中包含的排在了下面.

如果想按照排序顺序升序排列并且没有包含在排序值中的数据排在最后(使用最多的情况)如何写呢?

select * from sy_cd_ms_base_hospital_info order by FIELD(grade,'二甲','三丙','三乙','三甲') desc

把想要排序的降序写,再按降序排

 

标签:三乙,三丙,自定义,三甲,FIELD,二甲,mysql,排序
来源: https://www.cnblogs.com/1--2/p/15070468.html