MySQL按時間統計數據的方法總結


//查詢一周
格式:select * from 表名稱 where date_sub(curdate(), interval 6 day) <= date(表內時間字段);
語句:select * from garbage_collect  where date_sub(curdate(), interval 6 day) <= date(collection_time);

//查詢一個月
 格式:select * from 表名稱 where date_sub(curdate(), interval 1 month) <= date(表內時間字段);
 語句:select * from garbage_collect  where date_sub(curdate(),  interval 1 month) <= date(collection_time);

//查詢近半年
格式:select * from 表名稱 where date_sub(curdate(),  interval 6 month) <= date(表內時間字段);
語句:select * from garbage_collect  where date_sub(curdate(),  interval 6 month) <= date(collection_time);
//查詢2020年的數據:
格式:select * from 表名稱 where year(表名稱.表字段)="某年" 
語句:select * from garbage_collect where year(garbage_collect.collection_time)="2020" 
//查詢9月份的數據:
格式:select * from 表名稱 where year(表名稱.表字段)="某月" 
語句:select * from garbage_collect where month(garbage_collect.collection_time)="09" 
//查詢時期是08的數據:
格式:select * from 表名稱 where year(表名稱.表字段)="某日" 
語句:select * from garbage_collect where day(garbage_collect.collection_time)="08" 
//查詢2020年03月份的數據:
select * from garbage_collect where year(garbage_collect.collection_time)="2020" and month(garbage_collect.collection_time)="03"
//查詢2020年03月06日的數據:
select * from garbage_collect where year(garbage_collect.collection_time)="2020" and month(garbage_collect.collection_time)="03" and day(garbage_collect.collection_time)="06";
格式:select * from 表名稱 where 表字段 between "開始時間" and "結束時間";
語句:select * from garbage_collect where collection_time  between "2020-02-01" and "2020-03-31";

//查詢指定時間段數據(開始時間-結束時間)包含結束時間
語句:select * from garbage_collect where  collection_time  between "2020-03-17" and date_add("2020-03-17",interval 1 day)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM