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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章