mysql 按日期 Mysql 中按日期統計資料

2021-10-20 22:24:58 字數 1640 閱讀 8024

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)

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), 這裡星期一是星期的第一天

%% 乙個文字「%」。

如果在資料庫中,時間儲存的是 int 型別的時間戳,那麼需要將字串轉化為日期型別

from_unixtime()是mysql裡的時間函式,能將mysql的時間戳型別轉換成日期格式

因此,如果資料庫中時間報錯的事 int 型別的時間戳,那麼可以直接用下面的方式進行查詢

date_format(from_unixtime(create_time,"%y%m%d"),"%y%u")

完整的 sql 語句例如:

select date_format(from_unixtime(create_time,"%y%m%d"),"%y%u") weeks,count(caseid) count from tc_case group by weeks;

倘若僅僅是想要根據int 型別的時間戳來轉化成時間,可以如下操作:

select * from `order` where from_unixtime(`create_time`,"%y%m%d%h%i")='201706151639'

mysql按日期查詢資料 mysql按日期查詢資料

問題 mysql按日期查詢乙個月內的資料,查詢返回時結果集中只顯示有資料的結果 需求 查詢結果中假如當天沒有資料,自動填零並輸出 事件描述 sql語句 select date format date added,m.d as day,count product id as total from ht...

mysql 按日期分組

select date format now y m d days,count caseid count from tc case group by days date format是可以把一些時間格式轉化為你所需的時間格式,now 是2015 09 05 12 33 33,然後變為20150905...

MySql按日期進行統計

當天的資料 select from 表 where date 時間欄位名 curdate 當月的資料 select from 表 where date format 時間欄位名,y m date format curdate y m 昨天 select from 表名 where to days n...