我有一个条件查询:
所以,如果我有一张桌子
ID Order Last Name
1 0 Manner
2 1 Brock
3 0 Lester
4 0 Annual
5 0 Greatly
我期望的结果是:
Brock
Annual
Greatly
Lester
Manner
这是我的查询。每次发生的事情是我得到姓氏排序,而不是第一个非订单0:
select c.last_name
from person_reports crt
join person c
where c.org_id = 1000 and crt.reports_to_id = 100389 and c.id = crt.contact_id
order by c.last_name, case preference_num when 0 then 9999999 else preference_num end
我目前的工作成果:
Annual
Brock
Greatly
Lester
Manner
感谢您的帮助
答案 0 :(得分:3)
您可以简单地将ORDER BY子句反转为:
order by case preference_num when 0 then 9999999 else preference_num end, c.last_name
如果您在开头添加一个片段,则可以避免使用魔法'9999999':
order by case preference_num when 0 then 1 else 0 end, preference_num, c.last_name