mysql統計今天,昨天,本周,上周,本月,上月數據(mybatis版本)


create_time為創建時間
   <!--今天-->
    <sql id="today">
        and to_days(create_time)= to_days(now())
    </sql>

    <!--昨天-->
    <sql id="yesterday">
        and to_days(now())-to_days(create_time) <![CDATA[ <= ]]> 1
    </sql>

    <!--本周-->
    <sql id="thisWeek">
        and YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now())
    </sql>

    <!--上周-->
    <sql id="lastWeek">
        and YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now())-1
    </sql>

    <!--本月-->
    <sql id="thisMonth">
        and date_format(create_time,'%Y-%m')=date_format(now(),'%Y-%m')
    </sql>

    <!--上月-->
    <sql id="lastMonth">
        and date_format(create_time,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
    </sql>
b_share_info為分享表
b_look_info為查看表
   <!--查詢分享數-->
    <select id="queryShareCountByDayNumber" parameterType="java.lang.Integer" resultType="java.lang.Integer"
            useCache="false">
        select count(*) as shareCount
        from b_share_info
        where user_id = #{userId}
        <if test="dayNumber != null and dayNumber == 1">
            <include refid="today"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 2">
            <include refid="yesterday"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 3">
            <include refid="thisWeek"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 4">
            <include refid="lastWeek"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 5">
            <include refid="thisMonth"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 6">
            <include refid="lastMonth"></include>
        </if>
    </select>

    <!--查詢被查看數-->
    <select id="queryLookCountByDayNumber" parameterType="java.lang.Integer" resultType="java.lang.Integer"
            useCache="false">
        select sum(look_count) as lookCount
        from b_look_info
        where user_id = #{userId}
        <if test="dayNumber != null and dayNumber == 1">
            <include refid="today"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 2">
            <include refid="yesterday"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 3">
            <include refid="thisWeek"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 4">
            <include refid="lastWeek"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 5">
            <include refid="thisMonth"></include>
        </if>
        <if test="dayNumber != null and dayNumber == 6">
            <include refid="lastMonth"></include>
        </if>
    </select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM