您的位置:首页 > 数据库

mybatis分页多条件查询指定时间段数据的sql语句

2017-10-24 19:07 851 查看

  本案例适用于mysql数据库,在数据库中时间数据类型为datetime。

使用以下sql语句将不会报错,但也得不到想要的结果;

<select id="getGuidesList" parameterType="map" resultMap="GuideResultMap" flushCache="true">

     SELECT 

     a.guide_id,a.pat_id,a.direct_reason,a.sport_direct,a.food_direct,a.drug_suggest,a.guide_time,a.memo,b.name,c.doctorName

     from (pc_healthguide a LEFT  JOIN pc_patient b on a.pat_id=b.pat_id) LEFT  JOIN pc_doctor c on a.doctor_id=c.doc_id 

    <where>

      <if  test="doctorId!=null and doctorId !='0'">

           a.doctor_id=#{doctorId}

      </if>

      <if test="name != null and name!= ''">

           and  name =#{name}

       </if>

      <if  test="directReason!=null and directReason !=''">

         and  direct_reason=#{directReason}

      </if>

      <if test="guide_time!=null and guide_time!=''">
and guide_timebetween #{beginTime} and #{endTime}
</if>

 </where>

    <if test="start!=null and size!=null">

         limit #{start},#{size}

    </if> 

   </select>

项目运行后,select的sql语句where并没有拼接and createTime between #{beginTime} and #{endTime}这一串,正确用法这样才对:

<if  test="directReason!=null and directReason !=''">

         and  direct_reason=#{directReason}

      </if>

      <if test="beginDate!=null and beginDate!=''">
<![CDATA[   and DATE_FORMAT(guide_time, '%Y-%m-%d')>=  DATE_FORMAT(#{beginDate}, '%Y-%m-%d')   ]]>
</if>
  <if test="endDate!=null and endDate!=''">
<![CDATA[  and DATE_FORMAT(guide_time, '%Y-%m-%d') <= DATE_FORMAT(#{endDate}, '%Y-%m-%d')    ]]>
</if>

其中:beginDate和endDate实体类的属性,guide_time为数据库中的字段名,不要搞混淆了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐