【Mybatis】sql

select
        cu.reg_mobile mobile,
        substring_index(group_concat(ci.updated_user order by ci.updated_time desc), ',',1) as updatedUser,
        substring_index(group_concat(ci.remark order by ci.updated_time desc), ',',1) as remark,
        sum(ci.score) obtainScoreSum,
        sum(<![CDATA[case when ci.score > 0 then ci.score else 0 end]]>) scoreSum,
        sum(<![CDATA[case when ci.score < 0 then ci.score else 0 end]]>) usedScoreSum,
        count(<![CDATA[case when ci.score < 0 then ci.score else null end]]>) usedScoreNum,
        sum(case when ci.happen_way = 'SYS_GIFT' then ci.score else 0 end) sendScoreSum,
        count(ci.happen_way = 'SYS_GIFT' or null) sendScoreNum
        from t_vcar_cus_integral ci
        left join t_vcar_customer cu on cu.id = ci.customer_id
        group by cu.reg_mobile
        having 1=1

===================================================================================================

<select id="selectIdsForCustomerTask" resultType="java.lang.Long">
   SELECT customer.id
   FROM t_vcar_customer customer
   <where>
      customer.is_active = true
      AND customer.acc_type = 'CUST'
      <if test="registedBeforeTime != null">
         AND <![CDATA[customer.created_time <= #{registedBeforeTime} ]]>
      </if>
      <if test="noCoinChange != null and noCoinChange">
         AND NOT EXISTS (SELECT 1 FROM t_vcar_cus_integral integral WHERE integral.customer_id = customer.id
         <if test="startTime != null">
            AND <![CDATA[integral.created_time >= #{startTime}]]>
         </if>
         <if test="endTime != null">
            AND <![CDATA[integral.created_time <= #{endTime}]]>
         </if>
         )
      </if>
      <if test="noSign != null and noSign">
         AND NOT EXISTS (SELECT 1 FROM t_vcar_cus_sign sign WHERE sign.customer_id = customer.id
         <if test="startTime != null">
            AND <![CDATA[sign.created_time >= #{startTime}]]>
         </if>
         <if test="endTime != null">
            AND <![CDATA[sign.created_time <= #{endTime}]]>
         </if>
         )
      </if>
      <if test="noShare != null and noShare">
         AND NOT EXISTS (SELECT 1 FROM t_vcar_cus_active active WHERE active.customer_id = customer.id AND active.active_code in ('SINGLE_SHARE', 'DAY_SHARE', 'DAILY_SHARE') AND active.active_status = 'OVER'
         <if test="startTime != null">
            AND <![CDATA[active.created_time >= #{startTime}]]>
         </if>
         <if test="endTime != null">
            AND <![CDATA[active.created_time <= #{endTime}]]>
         </if>
         )
      </if>
      <if test="noOilCharge != null and noOilCharge">
         AND NOT EXISTS (SELECT 1 FROM t_vcar_cus_oil_charge charge WHERE charge.customer_id = customer.id
         <if test="startTime != null">
            AND <![CDATA[charge.created_time >= #{startTime}]]>
         </if>
         <if test="endTime != null">
            AND <![CDATA[charge.created_time <= #{endTime}]]>
         </if>
         )
      </if>
      <if test="noCompleteCarInfo != null and noCompleteCarInfo">
         AND NOT EXISTS (SELECT 1 FROM t_vcar_insure_relation relation, t_vcar_car_info car WHERE relation.customer_id = customer.id AND relation.car_info_id = car.id
         <if test="startTime != null">
            AND <![CDATA[car.created_time >= #{startTime}]]>
         </if>
         <if test="endTime != null">
            AND <![CDATA[car.created_time <= #{endTime}]]>
         </if>
         )
      </if>
   </where>
</select>

===================================================================================================

大小写模糊查询
<sql id="query_where">
   <where>
      <if test="inviteCode != null and inviteCode !=''">
         <![CDATA[and instr(UPPER(t.invite_code),UPPER(#{inviteCode})) > 0]]>
      </if>
      <if test="userId != null and userId !=''">
         and t.user_id like CONCAT('%',#{userId},'%')
      </if>
      <if test="userName != null and userName !=''">
         and t.user_name like CONCAT('%',#{userName},'%')
      </if>
      <if test="inviteType != null and inviteType !=''">
         and t.invite_type = #{inviteType}
      </if>
      <if test="activeFlag != null">
         and t.is_active = #{activeFlag}
      </if>
   </where>
</sql>

===================================================================================================

时间前后减少2个小时 DATE_SUB(#{变量名}, INTERVAL 2 HOUR)
<if test="carInfoStartTime != null">
    AND <![CDATA[car.created_time >= DATE_SUB(#{carInfoStartTime}, INTERVAL 2 HOUR)]]>
</if>

时间前后增加2个小时 DATE_ADD(#{变量名}, INTERVAL 2 HOUR)
<if test="carInfoEndTime != null">
    <![CDATA[max(car.created_time) <= DATE_ADD(#{carInfoEndTime}, INTERVAL 2 HOUR)]]> and
</if>

===================================================================================================

============================================================================================================================================================================================================================================================================================================================================================================================================

猜你喜欢

转载自blog.csdn.net/shiki_41/article/details/80166394