Mysql - 按自定义订购? [英] Mysql - order by custom?

查看:53
本文介绍了Mysql - 按自定义订购?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回以下数据的 mysql 查询:

I have a mysql query that is returning the following data:

select Desc,Value from table;

Desc        Value
 a           4
 r           3
 e           4
 j           7
 w           6
 p           6

我想按 Descr 排序,但要按特定顺序(json 需要此特定顺序)

I want to order by Descr but in a specific order (This specific is required for json)

Desc        Value
 r           3  
 a           4
 e           4
 j           7
 p           6
 w           6

这可能吗?

推荐答案

ORDER BY FIELD(`desc`, 'r', 'a', 'e', 'j', 'p', 'w')

注意:如果 desc 的值与给定的值不匹配,它将首先出现,因为 FIELD() 将返回 0

Note: if desc value doesn't match the given values, it will come first, as FIELD() would return 0

这篇关于Mysql - 按自定义订购?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆