zoukankan      html  css  js  c++  java
  • 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>
    一辈子很短,努力的做好两件事就好;第一件事是热爱生活,好好的去爱身边的人;第二件事是努力学习,在工作中取得不一样的成绩,实现自己的价值,而不是仅仅为了赚钱。
  • 相关阅读:
    WCF 第十章 异常处理 创建并使用强类型错误
    WCF 第十章 总结
    WCF 第十章 异常处理
    WCF 第十章 异常处理 使用FaultException管理服务异常
    WCF 第十章 异常处理 通信异常细节
    哪本书是对程序员最有影响、每个程序员都该阅读的书?(转自外刊IT评论)
    WCF 第十一章 工作流服务 从WF调用一个WCF服务
    比尔盖茨给大学毕业生的11条人生忠告
    王爽 汇编语言 实验七
    王爽 汇编语言 实验五第5题和第6题
  • 原文地址:https://www.cnblogs.com/antao/p/13696738.html
Copyright © 2011-2022 走看看