使用mysql查询当天、近一周、近一个月及近一年的数据以及各种报表查询sql

1.mysql查询当天的数据

|---|-----------------------------------------------------------------------|
| 1 | select * ``from table where to_days(时间字段) = to_days(now()); |

2.mysql查询昨天的数据

|---|-------------------------------------------------------------------------------|
| 1 | select * ``from table where to_days(now( ) ) - to_days( 时间字段名) <= 1 |

3.mysql查询近一个月的数据

|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 | SELECT * ``FROM table WHERE date``(时间字段) >= DATE_SUB(CURDATE(),INTERVAL 1 ``MONTH``)//查询近一个月 SELECT * ``FROM table WHERE date``(时间字段) >= DATE_SUB(CURDATE(),INTERVAL 1 ``YEAR) //查询近一年 SELECT * ``FROM table WHERE date``(时间字段) >= DATE_SUB(CURDATE(),INTERVAL 7 ``DAY) //查询近七天 |

4.mysql查询本月的数据

|---|---------------------------------------------------------------------------------------------------------------|
| 1 | select * ``from table where DATE_FORMAT(时间字段, ``'%Y%m' ) = DATE_FORMAT( CURDATE( ) , ``'%Y%m' ) |

5.mysql查询上月的数据

|---|--------------------------------------------------------------------------------------------------------------------|
| 1 | select * ``from table where PERIOD_DIFF(date_format(now(),``'%Y%m'``) , date_format(时间字段,``'%Y%m'``)) =1 |

6.mysql查询本年的数据

|---|---------------------------------------------------------------------|
| 1 | select * ``from table where YEAR``(时间字段)=``YEAR``(now()); |

7.mysql查询上一年的数据

|---|---------------------------------------------------------------------------------------------------|
| 1 | select * ``from table where year``(时间字段)=``year``(date_sub(now(),interval 1 ``year``)); |

8.mysql查询本周数据(周一为第一天)

|---|---------------------------------------------------------------------------------------------------------|
| 1 | SELECT * ``FROM table WHERE YEARWEEK(date_format(时间字段,``'%Y-%m-%d'``),1) = YEARWEEK(now(),1); |

9.mysql查询近五分钟的数据

|---|-------------------------------------------------------------------------------------|
| 1 | SELECT * ``FROM table WHERE 时间字段 >= DATE_SUB(now(),INTERVAL 5 ``MINUTE``) |

10.mysql查询某年度的每个月数据报表

      select a.date AS 'xData',IFNULL(b.sum, 0) AS 'yData'
        from  (
        select DATE_FORMAT(adddate(DATE_SUB(CURDATE(), INTERVAL dayofyear(now()) - 1 DAY),
        INTERVAL numlist.id - 1 month), '%m') as date
        from (SELECT @xi := @xi + 1 as id
        from (SELECT 1 UNION SELECT 2 UNION SELECT 3) xc1,
        (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc2,
        (SELECT @xi := 0) xc0) as numlist
        ) a
        left join
        (
        SELECT  IFNULL(SUM(income),0) sum, DATE_FORMAT(time, '%m') as date
        FROM zq_cnz_hy_income_report ts
        WHERE YEAR(time)=#{year}
        GROUP BY date ORDER BY date
        ) b
        on a.date = b.date order by a.date

效果图:

11.mysql查询近一年的每个月数据报表

        SELECT v.month AS 'xData',IFNULL(b.COUNT,0) AS 'yData' FROM (
        SELECT DATE_FORMAT(CURDATE(), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 1 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 2 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 3 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 4 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 5 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 6 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 7 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 8 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 9 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 10 MONTH), '%Y-%m') AS `month`
        UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%Y-%m') AS `month`
        ) v
        LEFT JOIN
        (
        SELECT LEFT(a.time,7) AS 'month',SUM(income) AS COUNT
        FROM zq_cnz_hy_income_report AS a
        LEFT JOIN zq_cnz_hy_user_station zs ON a.station_id=zs.station_id
        WHERE DATE_FORMAT(a.time,'%Y-%m')>DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 12 MONTH),'%Y-%m')
        GROUP BY MONTH) AS b
        ON v.month = b.month  GROUP BY v.month ORDER BY v.month

效果图:

12.mysql查询本月数据报表

       select a.date as 'xData',IFNULL(b.sum, 0) AS 'yData'
        from  (
        select date from (
        SELECT DATE_FORMAT(DATE_SUB(last_day(curdate()), INTERVAL xc-1 day), '%Y-%m-%d') as date
        FROM (
        SELECT @xi:=@xi+1 as xc from
        (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) xc1,
        (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) xc2,
        (SELECT @xi:=0) xc0
        ) xcxc) x0 where x0.date >= (select date_add(curdate(),interval-day(curdate())+1 day))
        ) a
        left join
        (
        select IFNULL(SUM(income),0) sum, DATE_FORMAT(time,'%Y-%m-%d') as date
        FROM zq_cnz_hy_income_report ts
        group by DATE_FORMAT(time, '%Y-%m-%d')
        ) b
        on a.date =b.date order by a.date

效果图:

13.mysql查询近一个月数据报表

SELECT
        b.created AS 'xData',
        IFNULL(c.sum, 0) AS 'yData'
        FROM
        (
        SELECT
        @cdate := date_add( @cdate, INTERVAL - 1 DAY ) created
        FROM
        ( SELECT @cdate := date_add( CURDATE( ), INTERVAL 1 DAY ) FROM zq_cnz_hy_income_report LIMIT 30 ) a
        ) b
        LEFT JOIN (
        select SUM(income) as sum, DATE_FORMAT(time,'%Y-%m-%d') as date
        FROM zq_cnz_hy_income_report ts
        group by DATE_FORMAT(time, '%Y-%m-%d')) c
        ON b.created = date_format( c.date, '%Y-%m-%d')
        GROUP BY
        b.created
        ORDER BY
        b.created;

效果图:

14.mysql查询近一周数据报表

 SELECT t1.datetime as 'xData', IFNULL(t2.num, 0) AS 'yData' FROM (
        SELECT CURDATE() AS datetime
        union all
        SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS datetime
        union all
        SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS datetime
        union all
        SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS datetime
        union all
        SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS datetime
        union all
        SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS datetime
        union all
        SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS datetime
        ) t1 LEFT JOIN (
        select IFNULL(SUM(income),0) num, DATE_FORMAT(time, '%Y-%m-%d') as datetime
        FROM zq_cnz_hy_income_report ts
        GROUP BY LEFT(datetime, 10) ORDER BY datetime DESC LIMIT 7
        ) t2 ON t1.datetime = t2.datetime ORDER BY t1.datetime asc

效果图:

相关推荐
易云码3 分钟前
信息安全建设方案,网络安全等保测评方案,等保技术解决方案,等保总体实施方案(Word原件)
数据库·物联网·安全·web安全·低代码
newxtc9 分钟前
【客观理性深入讨论国产中间件及数据库-科创基础软件】
数据库·中间件·国产数据库·国产中间件·科创
蓝眸少年CY9 分钟前
MySQL 【流程控制】函数
mysql
水月梦镜花11 分钟前
redis:list列表命令和内部编码
数据库·redis·list
MonkeyKing_sunyuhua1 小时前
ubuntu22.04 docker-compose安装postgresql数据库
数据库·docker·postgresql
天郁青1 小时前
数据库交互的本地项目:后台管理系统
数据库·交互
马剑威(威哥爱编程)1 小时前
MongoDB面试专题33道解析
数据库·mongodb·面试
小光学长1 小时前
基于vue框架的的流浪宠物救助系统25128(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
数据库·vue.js·宠物
掘金-我是哪吒1 小时前
微服务mysql,redis,elasticsearch, kibana,cassandra,mongodb, kafka
redis·mysql·mongodb·elasticsearch·微服务
零炻大礼包2 小时前
【SQL server】数据库远程连接配置
数据库