mysql查询之,本日,本年,本周,本月查询,按小时,日期,月份..进行统计

查询

日:

TO_DAYS(t1.create_time) =TO_DAYS(NOW())

周:
此处为美国周:周日-周六

YEARWEEK(date_format(t1.create_time,'%Y-%m-%d')) = YEARWEEK(now())

此处为中国周:周一-周日(将日期推迟一天)

YEARWEEK(date_format(t1.create_time,'%Y-%m-%d'),1) = YEARWEEK(now(),1)

月:

DATE_FORMAT(t1.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')

年:

YEAR(t1.create_time)=YEAR(NOW())

统计

按XX统计,必定是按XX来展示,所以这里直接将进行展示,然后GROUP BY 即可。
某时:

date_format(t1.create_time, '%H:00') days

某月某日:s

date_format(t1.create_time, '%m-%ds') days

某年某月:

date_format(t1.create_time, '%Y-%m') days
```s
某年某月某日:

```sqls
date_format(t1.create_time, '%Y-%m-%d') days

功能要求:统计模块的折线图的实现:
①查询条件:本日,本周,本月,本年
②展示:
本日条件下展示以小时为单位的数据
本周,本月展示以日为单位的数据
本年展示月为单位的数据
具体sql如下:(Mapper实现)
0:本日,2:本周,4:本月,7本年,20:自定义

SELECT
        <if test="param.code == 0">
            date_format(t1.create_time, '%H:00') days,
        </if>
        <if test="param.code == 2">
            date_format(t1.create_time, '%m-%d') days,
        </if>
        <if test="param.code == 4">
            date_format(t1.create_time, '%m-%d') days,
        </if>
        <if test="param.code == 7">
            date_format(t1.create_time, '%Y-%m') days,
        </if>
        <if test="param.code == 20">
            date_format(t1.create_time, '%Y-%m-%d') days,
        </if>
        sum(ifnull(t1.pay_price,0)) incomeAll,
        sum(ifnull(t1.pay_actual_price,0)) income,
        sum(ifnull(t1.use_consume,0)) con,
        sum(ifnull(t1.use_sun,0)) sun
        from sun_order_pay_detail t1
        <where>
            t1.accoun_type=1
            and t1.camp_id =  #{param.campId}
            <if test="param.code == 0">
                and  TO_DAYS(t1.create_time) =TO_DAYS(NOW())
            </if>
            <if test="param.code == 2">
                and  YEARWEEK(date_format(t1.create_time,'%Y-%m-%d'),1) = YEARWEEK(now(),1)
            </if>
            <if test="param.code == 4">
                and  DATE_FORMAT(t1.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')
            </if>
            <if test="param.code == 7">
                and  YEAR(t1.create_time)=YEAR(NOW())
            </if>
            <if test="param.code == 20">
                and t1.create_time &gt;= #{param.dayStartTime}
                and t1.create_time 	&lt;= #{param.dayEndTime}
            </if>
        </where>
        GROUP BY days

此sql最重要的是对mysql函数的使用具体表示参考下一篇

你可能感兴趣的:(mysql查询之,本日,本年,本周,本月查询,按小时,日期,月份..进行统计)