记一次多字段排序的sql使用if语句问题

  • 2019 年 10 月 10 日
  • 笔记

以上语句无法满足要求

实现如下:

1、mysql和oracle都支持

SELECT department_id,order_expected_amount,administrator_id, case administrator_id when 0 then -1 else order_expected_amount end as orderByNumber FROM order_calculate_collect WHERE `month`=4 ORDER BY department_id,orderByNumber desc;

2、只支持mysql

SELECT department_id,order_expected_amount,administrator_id FROM order_calculate_collect WHERE `month`=4 ORDER BY department_id,if(administrator_id = 0,1,0),order_expected_amount desc;

总结:验证了个事 if 这个只作用一行数据,我一直以为是if 作用完这一行 其他按着这个字段排序