1、查询startTime-endTime时间范围
(例:传过来的startTime=2020-12-30;endTime=2022-6-13)
(数据库中create_time=2021-12-13 15:14:49)
注:1、CONCAT()函数:是拼接日期+时间;
2、CAST()函数:将AS前面的数据转为datetime类型;
<where>
<if test="startTime != null and startTime != '' " >
and a.create_time <![CDATA[ >= ]]> CAST(CONCAT(#{startTime},' 00:00:00') AS datetime)
</if>
<if test="endTime != null and endTime != '' " >
and a.create_time <![CDATA[ <= ]]> CAST(CONCAT(#{endTime},' 23:59:59') AS datetime)
</if>
</where>
2、查询近一个月、近三个月、近一年时间内数据
(range=(1代表近一个月;2代表近3个月;3代表近一年))
注:1、>:是大于号的意思;
2、GETDATE():返回当前本地日期和时间;
<where>
<if test="range != null and range != '' " >
<choose>
<when test="range==1">
and a.create_time <![CDATA[ > ]]>; dateadd(mm, -1, GETDATE())
</when>
<when test="range==2">
and a.create_time > dateadd(mm, -3, GETDATE())
</when>
<when test="range==3">
and a.create_time > dateadd(mm, -12, GETDATE())
</when>
</choose>
</if>
</where>