java mybatis中order by多个条件判定实现职位置顶功能

错误代码
select a.*
from p a
LEFT JOIN h b on a.create_by=b.userid
order by if(a.top_position_end>NOW()) b.grade desc,a.create_date desc

p为职位表
create_by为职位发布人
create_date为职位发布时间
top_position_end 为职位置顶时间最后日期
h为条件表
grade 为条件值

要求现有日期小于职位置顶时间最后日期这个部分的职位按照grade的大小进行排序进行置顶,不满足日期小于职位置顶时间最后日期这个部分的职位在置顶职位之后,
按照create_date进行排序

查询语句中没有 m 表呢?grade 是 a 还是 b 表的呢?

如果order by支持case,可以试试这样

select a.*, Now() as current_time --不要把now放到条件里,因为每次扫描每条记录被执行的now肯定不一样
from p a
LEFT JOIN h b on a.create_by=b.userid
order by case when a.top_position_end>current_time then b.grade else 0 end desc, 
         case when a.top_position_end>current_time then '9999-12-31' else a.create_date end desc

如果current_time不认识,就用变量代替set @current_time=Now()

如果order by不支持case,可以用union

select a.*, Now() as current_time  
from p a
LEFT JOIN h b on a.create_by=b.userid
where a.top_position_end>current_time
order by b.grade desc
union all
select a.*, Now() as current_time
from p a
LEFT JOIN h b on a.create_by=b.userid
where a.top_position_end<=current_time
order by a.create_date desc