ORDER BY 有多个条件?

ORDER BY with multiple conditions?

我得到的产品数据如下所示:

id    order status    discount status    product type
------------------------------------------------------
1     in progress     pending approval   top
2     in progress     approved           top
3     NMI             pending approval   jeans
4     NMI             pending approval   jacket
5     completed       approved           helmet

有一个特定的产品排序顺序,类似于以下内容:

ORDER BY "order status" DESC, "discount status" ASC, "product type" DESC, "id" DESC

现在,这很好,但我需要在列表顶部有一个特定类型的订单,即 "order status" 正在进行和 "discount status" 待批准的订单。列表的其余部分应遵循上述顺序。

我知道我可以使用案例将这些信息拉到顶部,但我怎样才能做到这一点而不破坏其余的首选顺序?谢谢!

在标准SQL中,你可以这样写:

ORDER BY (CASE WHEN "order status" = 'in progress' AND "discount status" = 'pending approval'
               THEN 1 ELSE 2
          END),
         "order status" DESC,
         "discount status" ASC,
         "product type" DESC, "id" DESC

这应该适用于 MySQL 和 DB2。