Skip to content

Myabtis查询日期范围的三种方式

约 229 字小于 1 分钟

javaJDK

2024-12-25

方式一

 <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
      and p.create_time &gt;=#{startTime} AND  p.create_time &lt;=#{endTime}
 </if>

方式二

<if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
     and p.create_time between #{startTime} and #{endTime}
</if>

方式三

<if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
 
                <![CDATA[  and DATE_FORMAT(p.create_time, '%Y-%m-%d %H:%T:%s') >
                        DATE_FORMAT(#{startTime} , '%Y-%m-%d %H:%T:%s')    ]]>
 
                <![CDATA[  and DATE_FORMAT(p.create_time, '%Y-%m-%d %H:%T:%s') <=
                        DATE_FORMAT(#{endTime} , '%Y-%m-%d %H:%T:%s')    ]]>
</if>

总结:推荐方式一和方式和方式二,因为方式三使用了函数DATE_FORMAT,这样导致时间p.create_time索引失效;

其中between ..and ...相当于 [ >= <= ]的使用范围

Power by VuePress & vuepress-theme-plume