case when 条件查询的心得

mybatis最常用的应该是 if test的动态查询,但if语句里只能接收外部传参,而当需要把某字段当成条件去查询时,可以使用case when 语句(这个语法之前只在select用过,一般用来将一些枚举项的id转化为对应的汉字意思)
` rpr.count as count,
rpr.condition as condition_circle

    from  r_point_rule rpr
    LEFT JOIN r_point rp on rpr.id = rp.reason
    <where>
     rpr.is_delete = 0 and rp.is_delete = 0
     and rp.user_id = #{user_id} and
    (case when rpr.circle = 0 then 1=1 when rpr.circle = 1 then  date(rp.creat_time)=current_date
      when rpr.circle = 2 then YEARWEEK(date_format(rp.creat_time,'%Y-%m-%d'),1) = YEARWEEK(now(),1) end )
    </where>

    GROUP BY rpr.id ) as A    on A.id = rpr.id`
上一篇:循环缓存区的实现原理


下一篇:C++单线程智能指针实现