Mybatis按时间范围查询,Mybatis按年月日时分秒查询,MySQL按时间范围查询,MySQL年月日时分秒查询
更多实用源码 www.cx1314.cn
一、时间范围: 1 ==> 年; 2 ==> 月; 3 ==> 周
<select id="getBroadcastStatistics" resultType="com.rd.pojo.vo.BraodcastStatsVo">
SELECT
sum(bd.cog_id = '4') AS total,
sum(bd.proc_state = '1') AS ban,
sum(bd.proc_state = '2') AS disapper,
sum(bd.label_state = '0') AS new
FROM broadcast bd
DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= DATE(last_appeartime);
DATE_SUB(CURDATE(), INTERVAL 1 MONTH ) <= DATE(last_appeartime);
DATE_SUB(CURDATE(), INTERVAL 1 WEEK ) <= DATE(last_appeartime);
二、时间范围: : 0:今日,1:周,2:月,3:自定义,4:最近七天
更多实用源码 www.cx1314.cn
声明:classType------0:今日,1:周,2:月,3:自定义,4:最近七天
AND DATE_FORMAT(s.create_time,'%Y%u') = DATE_FORMAT(CURDATE( ),'%Y%u')
AND DATE_FORMAT(s.create_time,'%Y%m') = DATE_FORMAT(CURDATE( ),'%Y%m')
AND Date(s.create_time) between #{beginTime,jdbcType=VARCHAR} and CURDATE()
AND Date(s.create_time) <= #{endTime,jdbcType=VARCHAR}
AND Date(s.create_time) between #{beginTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
AND Date(s.create_time) = CURDATE()
AND date(s.create_time) between date_sub(curdate(), INTERVAL 6 DAY) and curdate()
AND Date(s.create_time) = CURDATE()
三、时间范围: y年,s季,m月 ,天
/*按照年度查询*/
AND Year(create_time) >=#{params.beginTime}
/**结束年度查询*/
AND Year(create_time) <= #{params.endTime}
更多实用源码 www.cx1314.cn
/**按照季度查询*/
AND concat(Year(create_time),quarter(create_time)) >= concat(Year(#{params.beginTime}),day(#{params.beginTime}))
/**结束季度查询*/
AND concat(Year(create_time),quarter(create_time)) <= concat(Year(#{params.endTime}),day(#{params.endTime}))
/**按照月份查询*/
AND date_format(create_time,'%y%m') >=date_format(#{params.beginTime},'%y%m')
/**结束月份查询*/
AND date_format(create_time,'%y%m') <= date_format(#{params.endTime},'%y%m')
AND date_format(create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
AND date_format(create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
四、时间范围: y年,s季,m月 ,其他
更多实用源码 www.cx1314.cn
AND Year(create_time) >=#{params.beginTime}
AND Year(create_time) <= #{params.endTime}
AND concat(Year(create_time),quarter(create_time)) >= concat(Year(#{params.beginTime}),day(#{params.beginTime}))
AND concat(Year(create_time),quarter(create_time)) <= concat(Year(#{params.endTime}),day(#{params.endTime}))
AND date_format(create_time,'%y%m') >=date_format(#{params.beginTime},'%y%m')
AND date_format(create_time,'%y%m') <= date_format(#{params.endTime},'%y%m')
AND date_format(create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
AND date_format(create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
五、传入时间范围参数类型是字符串
and date_format(create_time,'%Y-%m-%d %H:%i:%s') >= str_to_date(#{startTime},'%Y-%m-%d %H:%i:%s')
and date_format(create_time,'%Y-%m-%d %H:%i:%s') <= str_to_date(#{endTime},'%Y-%m-%d %H:%i:%s')
或者
and =DATE_ADD(#{beginTime },interval 1 day)]]>
and
六、传入时间范围参数类型是Date
and date_format(create_time,'%Y-%m-%d %H:%i:%s') >= date_format(#{startTime},'%Y-%m-%d %H:%i:%s')
and date_format(create_time,'%Y-%m-%d %H:%i:%s') <= date_format(#{endTime},'%Y-%m-%d %H:%i:%s')
七、Mybatis-Plus时间范围查询
更多实用源码 www.cx1314.cn
Page
IPage
new LambdaQueryWrapper
.apply(StrUtil.isNotBlank(start_date),
"date_format (create_time,'%Y-%m-%d %H:%i:%s') >= date_format('" + start_date + "','%Y-%m-%d %H:%i:%s')")
.apply(StrUtil.isNotBlank(end_date),
"date_format (create_time,'%Y-%m-%d %H:%i:%s') <= date_format('" + end_date + "','%Y-%m-%d %H:%i:%s')")
.orderByDesc(HmsFaceDetectLog::getOptime));
八、字符串转换成日期 、日期转换成字符串
//字符串转换成日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
更多实用源码 www.cx1314.cn
//日期转换成字符串
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(date);