mysql查詢每天每週每月每年的資料方法

2021-08-21 12:42:27 字數 1325 閱讀 2195

查詢每天的資料:

select

count(1) as countnumber,

date_format(createtime,'%y-%m-%d') as datetime

from

testtable

group by date_format(createtime,'%y-%m-%d')

查詢每週的資料:

select

count(1) as countnumber,

week(createtime) as datetime

from

testtable

group by week(createtime)

查詢每月的資料:

select

count(1) as countnumber,

month(createtime) as datetime

from

testtable

group by month(createtime)

查詢每年的資料:

select

count(1) as countnumber,

year(createtime) as datetime

from

testtable

group by year(createtime)

其它:date_add(createtime,interval 1 day)           得到指定日期後一天的日期/把1改為任意數字就可以得到後n天的日期

date_sub(createtime,interval 1 day)           得到指定日期前一天的日期/把1改為任意數字就可以得到前n天的日期

宣告變數查詢:

set @counter=0;

select @counter:=@counter+1 as id,count(*) as num from testtable order by num desc

Mysql查詢每天 每週 每月 每年的資料

查詢每天的資料 select count 1 as total,date format create time,y m d as time from op endor info group by date format create time,y m d 查詢每週的資料 select count 1...

mysql查詢每天每週每月每年的資料方法

查詢每天的資料 select count 1 as countnumber,date format createtime,y m d as datetime from testtable group by date format createtime,y m d 查詢每週的資料 select cou...

MYSQL統計每天 每週 每月 每年資料

select count 1 as total,date format created time,y m d as days from task execution group by days desc select count 1 as total,week created time as wee...