当前位置:   article > 正文

使用mysql查询当天、近一周、近一个月及近一年的数据以及各种报表查询sql_sql查询近一年每月的日期和数据

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查询某年度的每个月数据报表

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

效果图:

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

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

效果图:

 12.mysql查询本月数据报表

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

效果图:

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

  1. SELECT
  2. b.created AS 'xData',
  3. IFNULL(c.sum, 0) AS 'yData'
  4. FROM
  5. (
  6. SELECT
  7. @cdate := date_add( @cdate, INTERVAL - 1 DAY ) created
  8. FROM
  9. ( SELECT @cdate := date_add( CURDATE( ), INTERVAL 1 DAY ) FROM zq_cnz_hy_income_report LIMIT 30 ) a
  10. ) b
  11. LEFT JOIN (
  12. select SUM(income) as sum, DATE_FORMAT(time,'%Y-%m-%d') as date
  13. FROM zq_cnz_hy_income_report ts
  14. group by DATE_FORMAT(time, '%Y-%m-%d')) c
  15. ON b.created = date_format( c.date, '%Y-%m-%d')
  16. GROUP BY
  17. b.created
  18. ORDER BY
  19. b.created;

效果图:

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

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

效果图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/369216
推荐阅读
相关标签
  

闽ICP备14008679号