mysql 如何判断当前时间是否在 开始时间与结束时间之间 并且开始与结束时间允许为空
需求:查询进行中的活动数据
进行中一共有以下几种情况:
1.开始时间为空,结束时间为空, 此结果数据将永远为进行中的数据
2.开始时间为空,结束时间不为空,则当前时间在结束时间之前,为进行中的数据
3.开始时间不为空,结束时间为空,则当前时间在开始时间之后,为进行中的数据
4.开始时间不为空,结束时间不为空,则当前时间在开始与结束时间段之内的数据为进行中数据
下面sql则查询的是满足以上四种需求的结果集,达标题需求
SELECT * FROM
表名
WHERE 1=1
and(start_time is null or start_time<now()) and(end_time is null or end_time>now())
mybatis写法,开始时间与结束时间传入参数允许为空
如图所示:
<if test="record.startDate != null and record.startDate != '' or record.endDate != null and record.endDate != '' ">
AND id in
(select id from rht_product_price where 1=1
<if test="record.startDate != null and record.startDate != ''">
and start_date <= #{ record.startDate,jdbcType=VARCHAR}
</if>
<if test="record.endDate!= null and record.endDate != ''">
and end_date >= #{ record.endDate,jdbcType=VARCHAR}
</if>
)
</if>
还没有评论,来说两句吧...