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查询本周内每天统计量按天展示的示例代码的文章就介绍到这了,更多相关mysql统计量按天展示内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

(0)
上一篇 2022年7月1日
下一篇 2022年7月1日

相关推荐