网络编程
位置:首页>> 网络编程>> 数据库>> mysql判断当前时间是否在开始与结束时间之间且开始与结束时间允许为空

mysql判断当前时间是否在开始与结束时间之间且开始与结束时间允许为空

作者:纵有千千星晚  发布时间:2024-01-23 17:35:43 

标签: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写法,开始时间与结束时间传入参数允许为空
如图所示:

mysql判断当前时间是否在开始与结束时间之间且开始与结束时间允许为空


<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 &lt;= #{record.startDate,jdbcType=VARCHAR}
     </if>
     <if test="record.endDate!= null and record.endDate != ''">
       and end_date &gt;= #{record.endDate,jdbcType=VARCHAR}
     </if>
     )
   </if>

来源:https://blog.csdn.net/weixin_44980116/article/details/112677751

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com