按周,按月,按日,按小時分組統計資料

2021-09-02 15:54:54 字數 2849 閱讀 6422

[url]

遇到了,總結,僅僅是個搬運工!

來自陶偉基wiki

select date_format(create_time,'%y%u') weeks,count(caseid) count from tc_case group by weeks;

select date_format(create_time,'%y%m') months,count(caseid) count from tc_case group by months;

select date_format(create_time,'%y%m%d') days,count(caseid) count from tc_case group by days;

select date_format(create_time,'%y%m%d%h') hours,count(caseid) count from tc_case group by hours;

date_format(date,format)

根據format字串格式化date值。下列修飾符可以被用在format字串中:

%m 月名字(january……december)

%w 星期名字(sunday……saturday)

%d 有英語字首的月份的日期(1st, 2nd, 3rd, 等等。)

%y 年, 數字, 4 位

%y 年, 數字, 2 位

%a 縮寫的星期名字(sun……sat)

%d 月份中的天數, 數字(00……31)

%e 月份中的天數, 數字(0……31)

%m 月, 數字(01……12)

%c 月, 數字(1……12)

%b 縮寫的月份名字(jan……dec)

%j 一年中的天數(001……366)

%h 小時(00……23)

%k 小時(0……23)

%h 小時(01……12)

%i 小時(01……12)

%l 小時(1……12)

%i 分鐘, 數字(00……59)

%r 時間,12 小時(hh:mm:ss [ap]m)

%t 時間,24 小時(hh:mm:ss)

%s 秒(00……59)

%s 秒(00……59)

%p am或pm

%w 乙個星期中的天數(0=sunday ……6=saturday )

%u 星期(0……52), 這裡星期天是星期的第一天

%u 星期(0……52), 這裡星期一是星期的第一天

%% 乙個文字「%」。

來自danielyi

按年彙總,統計:

select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%y');

按月彙總,統計:

select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%y-%m');

按季度彙總,統計:

select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%y'),floor((date_format(col, '%m')+2)/3));

select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%y'),floor((date_format(col, '%m')+2)/3));

按小時:

select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by date_format(col, '%y-%m-%d %h ');

查詢 本年度的資料:

select * from mytable where year(from_unixtime(my_time)) = year(curdate())

查詢資料附帶季度數:

select id, quarter(from_unixtime(my_time)) from mytable;

查詢 本季度的資料:

select * from mytable where quarter(from_unixtime(my_time)) = quarter(curdate());

本月統計:

select * from mytable where month(my_time1) = month(curdate()) and year(my_time2) = year(curdate())

本週統計:

select * from mytable where month(my_time1) = month(curdate()) and week(my_time2) = week(curdate())

n天內記錄:

where to_days(now())-to_days(時間字段)<=n

按周,按月,按日,按小時分組統計資料

url 遇到了,總結,僅僅是個搬運工!來自陶偉基wiki select date format create time,y u weeks,count caseid count from tc case group by weeks select date format create time,y ...

MySql按周,按月,按日分組統計資料

select date format create time,y u weeks,count caseid count from tc case group byweeks select date format create time,y m d days,count caseid count fr...

MySql按周,按月,按日分組統計資料

select date format create time,y u weeks,count caseid count from tc case group by weeks select date format create time,y m d days,count caseid count f...