欢迎来到代码驿站!

Mysql

当前位置:首页 > 数据库 > Mysql

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

时间:2022-07-05 13:50:26|栏目:Mysql|点击:

本周

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中B树索引和B+树索引的区别详解

栏    目:Mysql

下一篇:介绍使用WordPress时10个常用的MySQL查询

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

本文地址:http://www.codeinn.net/misctech/206898.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有