A reference link:
https://blog.csdn.net/beidaol/article/details/86079157 (MyBatis by today, this week, this month filtering query)
https://blog.csdn.net/qq_41456723/article/details/100805761 (MyBatis (MySQL) - in | month | week period within the scope of the inquiry statistics)
https://blog.csdn.net/u010543785/article/details/52354957 (MySQL daily, weekly, monthly and yearly statistics sql statement consolidation, to achieve statistical reporting visualization)
https://blog.csdn.net/qq_39549434/article/details/78632984 (MyBatis query using the statistical function in the project for processing optimization)
https://www.cnblogs.com/huanghuanghui/p/9997041.html ( the mybatis reports, columns and dynamic query parameters + ranks of conversion )
https://blog.csdn.net/db_guy/article/details/78330529 (MySQL respectively, according to season, month, week, respectively statistics: concat () function)
https://blog.csdn.net/ydk888888/article/details/77965501 (Mysql, Mybatis make time to filter the report: Day Week Month statistics)
https://www.cnblogs.com/caoyc/p/5574948.html(Mybatis choose (when, otherwise)标签)
https://www.w3school.com.cn/sql/func_date_format.asp (MySQL the DATE_FORMAT () function)
1.if condition query
choose tags are sequentially determines its internal test condition when the tag is established whether, if there is a true, then choose ends.
/**
* 统计信息
* @param timeType 时间范围: 1 ==> 年; 2 ==> 月; 3 ==> 周;
* @return
*/
<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
<where>
<if test="timeType == 1">
DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= DATE(last_appeartime);
</if>
<if test="timeType == 2">
DATE_SUB(CURDATE(), INTERVAL 1 MONTH ) <= DATE(last_appeartime);
</if>
<if test="timeType == 3">
DATE_SUB(CURDATE(), INTERVAL 1 WEEK ) <= DATE(last_appeartime);
</if>
</where>
</select>
声明:classType------0:今日,1:周,2:月,3:自定义,4:最近七天
<choose>
<when test="classType == 1">
AND DATE_FORMAT(s.create_time,'%Y%u') = DATE_FORMAT(CURDATE( ),'%Y%u')
</when>
<when test="classType == 2">
AND DATE_FORMAT(s.create_time,'%Y%m') = DATE_FORMAT(CURDATE( ),'%Y%m')
</when>
<when test="classType == 3">
<choose>
<when test="beginTime!=null and beginTime!='' and endTime == '' ">
AND Date(s.create_time) between #{beginTime,jdbcType=VARCHAR} and CURDATE()
</when>
<when test="endTime!=null and endTime!='' and beginTime == '' ">
AND Date(s.create_time) <= #{endTime,jdbcType=VARCHAR}
</when>
<when test="beginTime!=null and beginTime!='' and endTime!=null and endTime!= '' ">
AND Date(s.create_time) between #{beginTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
</when>
<otherwise>
AND Date(s.create_time) = CURDATE()
</otherwise>
</choose>
</when>
<when test="classType == 4">
AND date(s.create_time) between date_sub(curdate(), INTERVAL 6 DAY) and curdate()
</when>
<otherwise>
AND Date(s.create_time) = CURDATE()
</otherwise>
</choose>
<if test="params.selected == 'y'.toString() and params.beginTime != null and params.beginTime != '' ">
AND Year(create_time) >=#{params.beginTime}
</if>/*按照年度查询*/
<if test="params.selected == 'y'.toString() and params.endTime != null and params.endTime != '' ">
AND Year(create_time) <= #{params.endTime}
</if>/**结束年度查询*/
<if test="params.selected == 's'.toString() and params.beginTime != null and params.beginTime != '' ">
AND concat(Year(create_time),quarter(create_time)) >= concat(Year(#{params.beginTime}),day(#{params.beginTime}))
</if>/**按照季度查询*/
<if test="params.selected == 's'.toString() and params.endTime != null and params.endTime != '' ">
AND concat(Year(create_time),quarter(create_time)) <= concat(Year(#{params.endTime}),day(#{params.endTime}))
</if> /**结束季度查询*/
<if test="params.selected == 'm'.toString() and params.beginTime != null and params.beginTime != '' ">
AND date_format(create_time,'%y%m') >=date_format(#{params.beginTime},'%y%m')
</if>/**按照月份查询*/
<if test="params.selected == 'm'.toString() and params.endTime != null and params.endTime != '' ">
AND date_format(create_time,'%y%m') <= date_format(#{params.endTime},'%y%m')
</if> /**结束月份查询*/
<if test="params.selected == 'd'.toString() and params.beginTime != null and params.beginTime != '' ">
AND date_format(create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
</if><!-- 开始时间检索 (按照天查询)-->
<if test="params.selected == 'd'.toString() and params.endTime != null and params.endTime != '' ">
AND date_format(create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
</if> <!-- 结束时间检索 (结束天查询)-->
2.choose condition query
<choose>
<when test="params.selected ='y'">
<if test="params.beginTime != null and params.beginTime != '' ">
AND Year(create_time) >=#{params.beginTime}
</if>
<if test="params.endTime != null and params.endTime != '' ">
AND Year(create_time) <= #{params.endTime}
</if>
</when>
<when test="params.selected ='s'">
<if test="params.beginTime != null and params.beginTime != '' ">
AND concat(Year(create_time),quarter(create_time)) >= concat(Year(#{params.beginTime}),day(#{params.beginTime}))
</if>
<if test="params.endTime != null and params.endTime != '' ">
AND concat(Year(create_time),quarter(create_time)) <= concat(Year(#{params.endTime}),day(#{params.endTime}))
</if>
</when>
<when test="params.selected ='m'">
<if test="params.beginTime != null and params.beginTime != '' ">
AND date_format(create_time,'%y%m') >=date_format(#{params.beginTime},'%y%m')
</if>
<if test="params.endTime != null and params.endTime != '' ">
AND date_format(create_time,'%y%m') <= date_format(#{params.endTime},'%y%m')
</if>
</when>
<otherwise>
<where>
<if test="params.beginTime != null and params.beginTime != '' ">
AND date_format(create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != '' ">
AND date_format(create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
</if>
</where>
</otherwise>
</choose>