MySQL:如何根据另一个字段',按ASC和DESC顺序对一些记录进行排序;s值
我有一桌“票”。MySQL:如何根据另一个字段',按ASC和DESC顺序对一些记录进行排序;s值,mysql,sql,sorting,sql-order-by,Mysql,Sql,Sorting,Sql Order By,我有一桌“票”。 表结构: 唯一ID(自动递增) 状态(打开/关闭/异常) 优先级(数字,较大的数字=较高的优先级) 创建日期 等等 我需要按以下顺序检索票据: 首先“打开”票证,按优先级排序(最高优先),然后按创建日期排序(最早优先) 接下来是“已关闭”票证,按创建日期排序(最新优先) 这可以通过两个查询的联合来完成,但这会增加很多复杂性 在单个查询中完成此操作有什么建议吗?不要使用union all。在排序依据中使用多个键: select t.* from t where statu
表结构:
- 唯一ID(自动递增)
- 状态(打开/关闭/异常)
- 优先级(数字,较大的数字=较高的优先级)
- 创建日期
- 等等
- 首先“打开”票证,按优先级排序(最高优先),然后按创建日期排序(最早优先)
- 接下来是“已关闭”票证,按创建日期排序(最新优先)
在单个查询中完成此操作有什么建议吗?不要使用
union all
。在排序依据中使用多个键:
select t.*
from t
where status in ('open', 'closed')
order by (status = 'open') desc,
(case when status = 'open' then priority end) desc,
(case when status = 'open' then creation_date end) asc,
(case when status = 'closed' then creation_date end) desc
不要使用
union all
。在排序依据中使用多个键:
select t.*
from t
where status in ('open', 'closed')
order by (status = 'open') desc,
(case when status = 'open' then priority end) desc,
(case when status = 'open' then creation_date end) asc,
(case when status = 'closed' then creation_date end) desc
完美的我不知道案例陈述可以这样使用。谢谢你的启发。太好了。我不知道案例陈述可以这样使用。谢谢你的启示。