mysql將資料按天,周,半月,月分組

2021-09-11 02:16:35 字數 993 閱讀 5558

最近有個需求,要求查出今天,一周,15天,乙個月的資料

created是時間戳

按天分組:

select sum(income) as income,

from_unixtime(created, '%y-%m-%d') as create_data

from `income`

group by create_data

order by create_data desc

按周分組

select sum(income) as income,

from_unixtime(created, '%y-%u') as create_data

from `income`

group by create_data

order by create_data desc

按半月分組

如果時間為 2019-01-1,代表上半個月,2019-01-2代表下半個月

select sum(income) as income,

if(from_unixtime(created, '%d') <= 15, from_unixtime(created, '%y-%m-1'), from_unixtime(created, '%y-%m-2')) as create_data

from `income`

group by create_data

order by create_data desc

按月分組

select sum(income) as income,

from_unixtime(created, '%y-%m') as create_data

from `income`

group by create_data

order by create_data desc

按天周月統計資料

mysql按周,按月,按日分組統計資料 2011 09 07 10 46 00 標籤 mysql 資料庫按周月日分組 統計資料 休閒select date format create time,y u weeks,count caseid count from tc case group by we...

Mysql 查詢天 周,月,季度 年的資料

今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1近7天 select from 表名 where date sub curdate interva...

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

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