一、建立測試資料二、sql查詢及示例
1.今天
select * from 表名 where to_days(時間欄位名) = to_days(now());2.昨天select * from demo s where to_days(s.create_time) = to_days(now());
select * from 表名 where to_days(now()) - to_days(時間欄位名) <= 1;3.本週select * from demo where to_days(now()) - to_days(create_time) <= 1;
select * from 表名 where yearweek(date_format(時間欄位名,'%y-%m-%d')) = yearweek(now());4.上週select * from demo where yearweek(date_format(create_time,'%y-%m-%d')) = yearweek(now());
select * from 表名 where yearweek(date_format(時間欄位名,'%y-%m-%d')) = yearweek(now())-1;5.近7天select * from demo where yearweek(date_format(create_time,'%y-%m-%d')) = yearweek(now())-1;
select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間欄位名);6.近30天select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名);7.本月select * from 表名 where date_format(時間欄位名,'%y%m') = date_format(curdate(),'%y%m');8.上月select * from 表名 where period_diff(date_format(now(),'%y%m'),date_format(時間欄位名,'%y%m')) = 1;9.近6個月select * from 表名 where date_format(時間欄位名,'%y%m') = date_format(curdate(),'%y%m') ;
select * from 表名 where weekofyear(from_unixtime(時間欄位名,'%y-%m-%d')) = weekofyear(now());
select * from 表名 where month(from_unixtime(時間欄位名,'%y-%m-%d')) = month(now());
select * from 表名 where year(from_unixtime(時間欄位名,'%y-%m-%d')) = year(now()) and month(from_unixtime(時間欄位名,'%y-%m-%d')) = month(now());
select * from 表名 where 時間欄位名 between date_sub(now(),interval 6 month) and now();10.本季度select * from 表名 where quarter(時間欄位名) = quarter(now());11.上季度select * from 表名 where quarter(時間欄位名) = quarter(date_sub(now(),interval 1 quarter));12.本年select * from 表名 where year(時間欄位名)=year(now());13.去年select * from 表名 where year(時間欄位名) = year(date_sub(now(),interval 1 year));
MySql查詢當天 本週 本月 本季度 本年的資料
select from 表名 where to days 時間欄位名 to days now select from 表名 where to days now to days 時間欄位名 1 select from 表名 where yearweek date format 時間欄位名,y m d ...
MySQL日期查詢 本週 本月 本季 本年
查詢當天 select from 表名 where to days 時間欄位名 to days now 查詢昨天 select from 表名 where to days now to days 時間欄位名 1 查詢一周 select from 表名 where date sub curdate i...
MySQL日期查詢 本週 本月 本季 本年
查詢當天 select from 表名 where to days 時間欄位名 to days now 查詢昨天 select from 表名 where to days now to days 時間欄位名 1 查詢一周 select from 表名 where date sub curdate i...