文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

mysql查询本周内每天统计量按天展示的示例代码

2024-04-02 19:55

关注

本周

SELECT 
  b.item,IFNULL(a.COUNT,0) AS VALUE
FROM (
  SELECT  DATE(subdate(curdate(),date_format(curdate(),'%w')-1)) as item  
            union all  
            SELECT  DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 1 day)) as item  
            union all  
            SELECT  DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 2 day)) as item  
            union all  
            SELECT  DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 3 day)) as item  
            union all  
            SELECT  DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 4 day)) as item  
            union all  
            SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 5 day)) as item  
            union all  
            SELECT DATE(DATE_ADD(subdate(curdate(),date_format(curdate(),'%w')-1), interval 6 day)) as item
) b
LEFT JOIN
(
  SELECT DATE_FORMAT(create_time,'%Y-%m-%d') days, COUNT(*) COUNT 
    FROM (SELECT * FROM `table`  WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(create_time)) as c
  GROUP BY days
) AS a    
ON (b.item = a.days)

前七天

SELECT 
  b.item,IFNULL(a.COUNT,0) AS VALUE
FROM (
  SELECT CURDATE() AS item
  UNION ALL
  SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS item
  UNION ALL
  SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS item
  UNION ALL
  SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS item
  UNION ALL
  SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS item
  UNION ALL
  SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS item
  UNION ALL
  SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS item
) b
LEFT JOIN
(
  SELECT DATE_FORMAT(create_time,'%Y-%m-%d') days, COUNT(*) COUNT 
    FROM (SELECT * FROM `table`  WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(create_time)) as c
  GROUP BY days
) AS a    
ON (b.item = a.days)

本月

SELECT
    `type`,
    max( `count` ) AS `count` 
FROM
    (
    SELECT
        count(*) AS `count`,
        DATE_FORMAT( create_time, '%Y-%m-%d' ) AS `type` 
    FROM
        `table` a 
    WHERE
        DATE_FORMAT( create_time, '%Y%m' ) = DATE_FORMAT( CURDATE(), '%Y%m' ) 
    GROUP BY
        `type` UNION ALL
    SELECT
        0 AS `copunt`,
        @cdate := date_add( @cdate, INTERVAL - 1 DAY ) `type` 
    FROM
        ( SELECT @cdate := date_add( last_day( curdate()), INTERVAL + 1 DAY ) FROM `table` ) t1 
    WHERE
        @cdate > (
        date_add( curdate(), INTERVAL - DAY ( curdate())+ 1 DAY )) 
    ) _tmpAllTable 
GROUP BY
    `type`

本年按月展示

SELECT
    CONCAT(
        YEAR ( click_date ),
        '-',
    MONTH ( click_date )) AS `type`,
    IFNULL( b.con, 0 ) AS `count` 
FROM
    (
    SELECT
        STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 1 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 2 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 3 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 4 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 5 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 6 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 7 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 8 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 9 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 10 MONTH ) AS click_date UNION ALL
    SELECT
        DATE_ADD( STR_TO_DATE( CONCAT( YEAR ( CURDATE()), '-', 1, '-', 1 ), '%Y-%m-%d' ), INTERVAL 11 MONTH ) AS click_date 
    ) a
    LEFT JOIN ( SELECT COUNT(*) AS con, CONCAT( YEAR ( REPORTDATE ), '-', MONTH ( REPORTDATE )) AS mon FROM `ls172_workorder` GROUP BY mon ) b ON CONCAT(
        YEAR ( click_date ),
    '-',
    MONTH ( click_date ))= b.mon

到此这篇关于mysql查询本周内每天统计量按天展示的示例代码的文章就介绍到这了,更多相关mysql统计量按天展示内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯