Mybatis按年月日时分秒查询,MySQL年月日时分秒查询
Mybatis按时间范围查询,Mybatis按年月日时分秒查询,MySQL按时间范围查询,MySQL年月日时分秒查询
一、时间范围: 1 ==> 年; 2 ==> 月; 3 ==> 周
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:最近七天
声明: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}
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月 ,其他
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时间范围查询
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();
}
//日期转换成字符串
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(date);
来源地址:https://blog.csdn.net/qzc2017/article/details/126958545
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341