Mysql中datetime条件查询与Mybatis条件忽略时分秒查询

Mysql中的时间 一般常用的两个 ,有date 和datetime 两个的区别是

date:该类型表示年-月-日,标准格式为YYYY-MM-DD,
datetime:该类型表示YYYY-MM-DD hh:mm:ss,
所以能 一般用查date类型直接2019-01-01 就可以 但是用datetime 在用这种方式查是查不全的,应为他有具体的时分秒,特别是条件查询 开始时间 和结束时间 如果是 2019-01至01-2019-01-20的数据 查出来 是不包含2019-01-20那条数据的数据的,所以 date_format(时间,%Y-%m-%d00:00:00’)解决这个问题

Mysql:

现在的时间的数据
在这里插入图片描述

如果直接查发现只查出两条 见上图在2019-11-18至2019-11-20的数据有四条才对

在这里插入图片描述

使用date_format(时间,%Y-%m-%d00:00:00’)查询就可以得到4条数

select * from s_cetc_order
where 
 date_format(submissionTime,'%Y-%m-%d00:00:00')>=date_format('2019-11-18','%Y-%m-%d00:00:00')
 AND 
 date_format(submissionTime,'%Y-%m-%d00:00:00')<=date_format('2019-11-20','%Y-%m-%d23:59:59')

在这里插入图片描述

Mybatis条件

这就条件因需求而定
第一种

 <if test="startime !='' and  startime != null)">and  date_format( a.submissionTime,'%Y-%m-%d00:00:00') >=date_format(#{startime},'%Y-%m-%d00:00:00')</if>
  <if test="(endtime != ''and endtime != null">and date_format(a.submissionTime,'%Y-%m-%d00:00:00') &lt;= date_format(#{endtime},'%Y-%m-%d23:59:59')</if>

第二种

 <if test="(startime !='' and  startime != null) and (endtime == '' or endtime == null)">and  date_format( a.submissionTime,'%Y-%m-%d00:00:00') >=date_format(#{startime},'%Y-%m-%d00:00:00')</if>
        <if test="(startime ==''or startime == null) and (endtime != ''and endtime != null)">and date_format(a.submissionTime,'%Y-%m-%d00:00:00') &lt;= date_format(#{endtime},'%Y-%m-%d23:59:59')</if>
        <if test=" (startime != null and startime != '') and (endtime != null and endtime != '')">
        and  date_format(a.submissionTime,'%Y-%m-%d00:00:00') between  date_format(#{startime},'%Y-%m-%d00:00:00') and date_format(#{endtime},'%Y-%m-%d23:59:59') </if>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

  T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值