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`
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值