Mybatis中按照年、季度、月、日查询

一.参考链接:

https://blog.csdn.net/beidaol/article/details/86079157(MyBatis按今天、本周、本月过滤查询)

https://blog.csdn.net/qq_41456723/article/details/100805761(MyBatis(MySQL)-- 年|月|周时间范围内查询统计)

https://blog.csdn.net/u010543785/article/details/52354957(mysql 按日、周、月、年统计sql语句整理,实现报表统计可视化)

https://blog.csdn.net/qq_39549434/article/details/78632984(使用MyBatis对项目中的统计功能进行处理的查询语句优化)

https://www.cnblogs.com/huanghuanghui/p/9997041.htmlmybatis报表,动态列与查询参数+行列转换

https://blog.csdn.net/db_guy/article/details/78330529(mysql分别按照季,月,周分别统计数据:concat()函数)

https://blog.csdn.net/ydk888888/article/details/77965501(Mysql,Mybatis做时间过滤报表:日周月统计)

https://www.cnblogs.com/caoyc/p/5574948.htmlMybatis choose (when, otherwise)标签

https://www.w3school.com.cn/sql/func_date_format.asp(MySQL DATE_FORMAT() 函数)

1.if条件查询

choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。

    /**
     * 统计信息
     * @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) &lt;= DATE(last_appeartime);
            </if>
            <if test="timeType == 2">
                DATE_SUB(CURDATE(), INTERVAL 1 MONTH ) &lt;= DATE(last_appeartime);
            </if>
            <if test="timeType == 3">
                DATE_SUB(CURDATE(), INTERVAL 1 WEEK ) &lt;= 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) &gt;=#{params.beginTime}
            </if>/*按照年度查询*/
            <if test="params.selected == 'y'.toString() and params.endTime != null and params.endTime != '' ">
                AND Year(create_time) &lt;= #{params.endTime}
            </if>/**结束年度查询*/
            <if test="params.selected == 's'.toString() and params.beginTime != null and params.beginTime != '' ">
                AND concat(Year(create_time),quarter(create_time)) &gt;= 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)) &lt;= 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') &gt;=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') &lt;= 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') &gt;= 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') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>  <!-- 结束时间检索 (结束天查询)-->

2.choose条件查询

 <choose>
                <when test="params.selected ='y'">
                    <if test="params.beginTime != null and params.beginTime != '' ">
                        AND Year(create_time) &gt;=#{params.beginTime}
                    </if>
                    <if test="params.endTime != null and params.endTime != '' ">
                        AND Year(create_time) &lt;= #{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)) &gt;= concat(Year(#{params.beginTime}),day(#{params.beginTime}))
                    </if>
                    <if test="params.endTime != null and params.endTime != '' ">
                        AND concat(Year(create_time),quarter(create_time)) &lt;= 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') &gt;=date_format(#{params.beginTime},'%y%m')
                    </if>
                    <if test="params.endTime != null and params.endTime != '' ">
                        AND date_format(create_time,'%y%m') &lt;= 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') &gt;= date_format(#{params.beginTime},'%y%m%d')
                        </if>
                        <if test="params.endTime != null and params.endTime != '' ">
                            AND date_format(create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
                        </if>
                    </where>
                </otherwise>
            </choose>

 

发布了95 篇原创文章 · 获赞 44 · 访问量 20万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章