How do I write a query with a conditional order by?
I currently have a 开发者_StackOverflowquery that is built based on order details passed from a POST method. I also need to have a default order in addition that kicks records to the end of the results that have one of two fields equal to specific values, respectively.
Basically, despite the passed dynamic order, if a record's status_hold is not null OR stage_num=17 then the record needs to be at the end of the list.
You can use a case statement as part of your sort:
order by case when status_hold is not null OR stage_num=17 then 1 else 0 end
精彩评论