• MYSQL 窗体汇总函数


            如果我们想要汇总当天数据,当月数据,当年数据的。如果不懂窗体函数,可能会比较费劲,那小编就说了,我用java处理同样可以达到效果啊。可问题是。明明有现成的函数,为啥要用java处理,当然同时,java会不停的判断时间范围,严重影响性能。

            当我们掌握了一下函数使用时,

    ①,提高工作效率

    ②,防止过多bug

    ③,提升项目性能

    ④, 适用于各种大屏汇总等场景

    首先我们来看一下场景

    表中数据每小时会有一条数据,time是每小时一次数据vachar(2)类型,

    一、如果我想汇总当天的数据呢?

    1. SELECT
    2. SUM( visitors )
    3. FROM
    4. your_table
    5. WHERE
    6. website_id = '8a87d34b8a2c0216018a2c043a220003'
    7. AND DATE( create_time ) = CURDATE();

    二、如果我想汇总当月的数据呢?

    1. SELECT
    2. time,
    3. vistros,
    4. SUM( vistros) OVER ( ORDER BY time ) AS total_vistros
    5. FROM
    6. (
    7. SELECT
    8. DATE_FORMAT( time, '%Y-%m-%d' ) AS time,
    9. sum( visitors ) vistros
    10. FROM
    11. your_table
    12. WHERE
    13. website_id = '8a87d34b8a2c0216018a2c043a220003'
    14. AND DATE_FORMAT( time, '%Y%m' )= DATE_FORMAT( CURDATE(), '%Y%m' )
    15. GROUP BY
    16. DATE_FORMAT( time, '%Y-%m-%d' )
    17. ) t1
    18. ORDER BY
    19. time ASC

    三、如果是当年呢?

    1. SELECT MONTH AS month,
    2. vistros,
    3. SUM( vistros ) OVER ( ORDER BY MONTH ) AS total_vistros
    4. FROM
    5. (
    6. SELECT
    7. DATE_FORMAT( time, '%Y-%m' ) AS MONTH,
    8. SUM( visitors ) AS vistros
    9. FROM
    10. your_table
    11. WHERE
    12. website_id = '8a87d34b8a2c0216018a2c043a220003'
    13. AND YEAR ( time )= YEAR (now())
    14. GROUP BY MONTH
    15. ORDER BY MONTH DESC
    16. ) t1
    17. ORDER BY
    18. MONTH ASC;

    四、日期与时间查询语句:查询当年、查询本年、查询当月、查询本周

    1. select sum(1) as years,
    2. (select sum(1) from `表名` where date_format( 时间字段名, '%y%m' ) = date_format( curdate( ) , '%y%m')) as months,
    3. (select sum(1) from `表名` where to_days(时间字段名) = to_days(now()))as days
    4. from `表名` where year(regtime)=year(now())
    5. #当天
    6. select * from 表名 where to_days(时间字段名) = to_days(now());
    7. #昨天
    8. select * from 表名 where to_days( now( ) ) – to_days( 时间字段名) <= 1
    9. #一周
    10. select * from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段名)
    11. #近30select * from 表名 where date_sub(curdate(), interval 30 day) <= date(时间字段名)
    12. #本月
    13. select * from 表名 where date_format( 时间字段名, ‘%y%m’ ) = date_format( curdate( ) , ‘%y%m’ )
    14. #上一月
    15. select * from 表名 where period_diff( date_format( now( ) , ‘%y%m’ ) , date_format( 时间字段名, ‘%y%m’ ) ) =1
    16. #查询本周记录
    17. select * from ht_invoice_information where weekofyear(create_date)=weekofyear(now());
    18. #查询上周记录
    19. select * from ht_invoice_information where create_date>=date_add(now(),interval -(8 + weekday(now())) day) and create_date<=date_add(now(),interval -(1 + weekday(now())) day);
    20. --//或者
    21. select * from `ht_invoice_information` where weekofyear(create_date)=weekofyear(date_sub(now(),interval 1 week));
    22. #查询本月数据
    23. select * from ht_invoice_information where month(create_date)=month(now()) and year(create_date)=year(now());
    24. #查询上月数据
    25. select * from ht_invoice_information where create_date<=last_day(date_add(now(),interval -1 month)) and create_date>=date_format(concat(extract(year_month from date_add(now(),interval -1 month)),’01′),’%y-%m-%d’);
    26. --//或者
    27. select * from `ht_invoice_information` where month(create_date)=month(date_sub(now(),interval 1 month))
    28. and year(create_date)=year(now());
    29. #查询本季度数据
    30. select * from `ht_invoice_information` where quarter(create_date)=quarter(now());
    31. #查询上季度数据
    32. select * from `ht_invoice_information` where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));
    33. #查询本年数据
    34. select * from `ht_invoice_information` where year(create_date)=year(now());
    35. #查询上年数据
    36. select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

    参考资料

    MySQL中按月统计并逐月累加统计值的几种写法-51CTO.COM

    mysql日期与时间查询语句:查询当年、查询本年、查询当月、查询本周_mysql 判断日期是某月份-CSDN博客 https://www.cnblogs.com/littlebob/p/12916389.html

  • 相关阅读:
    十、Spring Boot 安全管理(2)
    世界前沿技术发展报告2023《世界航天技术发展报告》(五)太空探索技术
    (二)随机变量的数字特征:探索概率分布的关键指标
    在腾讯做了4年软件测试,来面试要求35k,让我见识到了真正的软件测试天花板...
    Streaming Systems
    如何做网站推广-外链为皇
    一个失败的案例
    NFT,还是有未来的
    公共命名空间中的依赖关系
    Office Word如何设置页码(MacOS中为例)
  • 原文地址:https://blog.csdn.net/L___Ping/article/details/132871277