時間轉化,按周,按日,按時,按年統計必備 html
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 from tc_case group by days; select DATE_FORMAT(create_time,'%Y%m') months,count(caseid) count from tc_case group by months; DATE_FORMAT(date,format) 根據format字符串格式化date值。下列修飾符能夠被用在format字符串中: %M 月名字(January……December) %W 星期名字(Sunday……Saturday) %D 有英語前綴的月份的日期(1st, 2nd, 3rd, 等等。) %Y 年, 數字, 4 位 www.2cto.com %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), 這裏星期一是星期的第一天 %% 一個文字「%」。
表結構 id type 001 1 001 0 002 1 001 0 002 0 001 1 001 0 002 0 要求: 統計不一樣id,type分別爲0的,1的個數。 sql語句: select id,sum(case when type=0 then 1 else 0 end) as '0',sum(case when type=1 then 1 else 0 end) as '1' from t group by id 查詢結果: id 0 1 001 3 2 002 2 1 分享:
應用:這個是按周查詢mysql
select STR_TO_DATE(concat(recordTim,daytime+1),'%X%V%w') as recordTime,seccessNum,failureNum,totalNum from (select yearweek(visitTime) as recordTim,weekday(visitTime) as daytime, sum(case when visitStatus=1 then 1 else 0 end) as seccessNum, sum(case when visitStatus=0 then 1 else 0 end) as failureNum ,count(*) as totalNum from kbs_applog where accessSecret="12d5ew5d66gf" and visitTime >="2016-12-16 11:11:06.0" and visitTime <= "2016-12-20 11:16:06.0" group by recordTim order by recordTim) as temtable limit 1,10
按日查詢:sql
select date_format(visitTime,"%Y-%m-%d") as recordTime, sum(case when visitStatus=1 then 1 else 0 end) as seccessNum, sum(case when visitStatus=0 then 1 else 0 end) as failureNum , count(*) as totalNum from kbs_applog where accessSecret="12d5ew5d66gf" and visitTime between "2016-12-16 11:11:06" and "2016-12-20 11:16:06" group by recordTime
按時查詢app
select date_format(visitTime,"%Y-%m-%d %H") as recordTime, sum(case when visitStatus=1 then 1 else 0 end) as seccessNum, sum(case when visitStatus=0 then 1 else 0 end) as failureNum , count(*) as totalNum from kbs_applog where accessSecret="12d5ew5d66gf" and visitTime between "2016-12-16 11:11:06" and "2016-12-20 11:16:06" group by recordTime
更多函數參考:函數
http://blog.chinaunix.net/uid-11600035-id-2866132.htmlui
http://www.cnblogs.com/ggjucheng/p/3352280.htmlspa