MySql查詢當天 本週 本月 本季度 本年的資料

2021-10-03 19:35:07 字數 1675 閱讀 7443

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')) = yearweek(now());
select * from 表名 where yearweek(date_format(時間欄位名,'%y-%m-%d')) = yearweek(now())-1;
select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間欄位名);
select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名);
select * from 表名 where date_format(時間欄位名,'%y%m') = date_format(curdate(),'%y%m');
select * from 表名 where period_diff(date_format(now(),'%y%m'),date_format(時間欄位名,'%y%m')) = 1;

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();
select * from 表名 where quarter(時間欄位名) = quarter(now());
select * from 表名 where quarter(時間欄位名) = quarter(date_sub(now(),interval 1 quarter));
select * from 表名 where year(時間欄位名)=year(now());
select * from 表名 where year(時間欄位名) = year(date_sub(now(),interval 1 year));

MySql查詢當天 本週 本月 本季度 本年的資料

一 建立測試資料 二 sql查詢及示例 1.今天 select from 表名 where to days 時間欄位名 to days now select from demo s where to days s.create time to days now 2.昨天 select from 表名...

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...