I have a stored procedure that fetches data and currently orders it as follows:

ORDER BY    dateEsc desc, prio desc, EID desc

Is there a way that I can say IF prio = "Urgent" THEN put such a record on the very top of my results and only for the remaining records apply the above order ?

有帮助吗?

解决方案

Something like the following will work, because when prio = 'Urgent' then the new condition will generate 0 else 1 and sort by this value first; and later sort by other conditions...

ORDER BY CASE WHEN prio = "Urgent" THEN 0 ELSE 1 END,
    dateEsc desc, prio desc, EID desc
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top