mysql 按時間段統計(年,季度,月,天,時)

2021-06-27 06:40:10 字數 1305 閱讀 7317

按年彙總,統計:

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

Oracle 按時間段分組統計

想要按時間段分組查詢,首先要了解level,connect by,oracle時間的加減.關於level這裡不多說,我只寫出乙個查詢語句 sql view plain copy level 是乙個偽例 select level from dual connect bylevel 10 結果 1 2 ...

MySql 按時間段查詢資料方法

格式為2008 06 16 查詢出當天資料 select from table where date 時間字段 curdate 查詢出當月字段 select from table where month 時間字段 month now 時間格式為1219876 unix時間,只要應用 from uni...

MySql 按時間段查詢資料方法

格式為2008 06 16 查詢出當天資料 select from table where date 時間字段 curdate 查詢出當月字段 select from table where month 時間字段 month now 時間格式為1219876 unix時間,只要應用 from uni...