本文主要是介绍Mysql 按当天、当月、上月及按日期范围查询 DATE_FORMAT( date, ‘%Y%m‘ ),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
//1、根据开始时间结束时间查询
SELECT *
FROM
record
WHERE 1=1
<if test="dateEnd != null and dateEnd != ''"> AND operation_time <![CDATA[ < ]]> str_to_date(#{dateEnd},'%Y-%m-%d %H:%i:%s')</if>
<if test="dateStart != null and dateStart != ''"> AND operation_time <![CDATA[ >= ]]> str_to_date(#{dateStart},'%Y-%m-%d %H:%i:%s')</if>
ORDER BY id DESC
//2、查询当天数据
SELECT *
FROM
record
WHERE 1=1
AND DATE_FORMAT( operation_time, '%Y%m%d' ) = DATE_FORMAT( CURDATE() , '%Y%m%d' )
ORDER BY id DESC
//3、查询当月数据
SELECT *
FROM
record
WHERE 1=1
AND DATE_FORMAT( operation_time, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' )
ORDER BY id DESC
//4、查询上个月数据
SELECT *
FROM
record
WHERE 1=1
AND PERIOD_DIFF( DATE_FORMAT( NOW( ) , '%Y%m' ) , DATE_FORMAT( operation_time, '%Y%m' ) ) =1
ORDER BY id DESC
这篇关于Mysql 按当天、当月、上月及按日期范围查询 DATE_FORMAT( date, ‘%Y%m‘ )的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!