MySQL按多个case语句排序 [英] MySQL order by multiple case statements

查看:597
本文介绍了MySQL按多个case语句排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我需要按记录的状态"对记录进行排序,状态"由字段的组合组成.这是一个示例,它应如何返回按状态升序排列的结果:

I have a situation where I need to sort my records by their "status" which is made up of a combination of fields. Here is an example how it should return the results sorted by status in ascending order:

        |     Sent      Received         Approved
--------------------------------------------------
record1 |     null        null             null
record2 |  2012-01-01     null             null
record3 |  2012-01-01   2012-01-01         null
record4 |  2012-01-01   2012-01-01      2012-01-01

我将如何创建一个MySQL查询,以这些记录的整体状态"对这些记录进行排序?

How would I create a MySQL query that would order these records by their overall "status"?

推荐答案

order by
   case when sent is null and received is null and approved is null then 1
        when received is null and approved is null then 2
        when approved is null then 3
        else 4 end

这篇关于MySQL按多个case语句排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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