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') <= 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') <= 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>