我有类似这样的产品的数据:
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
Run Code Online (Sandbox Code Playgroud)
产品通常按特定顺序排序,类似于以下内容:
ORDER BY "order status" DESC, "discount status" ASC, "product type" DESC, "id" DESC
Run Code Online (Sandbox Code Playgroud)
现在,这很好,但我需要列表顶部的特定类型的订单,即“订单状态”正在进行中和“折扣状态”待批准的订单。列表的其余部分应遵循上述顺序。
我知道我可以使用案例将这些信息拉到顶部,但是如何在不破坏其余首选顺序的情况下做到这一点?谢谢!
在标准 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
Run Code Online (Sandbox Code Playgroud)
这应该适用于 MySQL 和 DB2。
归档时间: |
|
查看次数: |
3185 次 |
最近记录: |