MySQL由多个case语句排序

时间:2012-02-27 17:18:19

标签: mysql

我遇到的情况是,我需要按照“状态”对记录进行排序,“状态”由字段组合组成。下面是一个示例,它应该如何按升序顺序返回按状态排序的结果:

        |     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查询,按照整体“状态”对这些记录进行排序?

1 个答案:

答案 0 :(得分:11)

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