Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:如何根据另一个字段',按ASC和DESC顺序对一些记录进行排序;s值_Mysql_Sql_Sorting_Sql Order By - Fatal编程技术网

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

完美的我不知道案例陈述可以这样使用。谢谢你的启发。太好了。我不知道案例陈述可以这样使用。谢谢你的启示。