MySQL根据日期查询 发表于 2018-10-21 | | 阅读次数: MySQL根据日期查询。此处并没有考虑性能问题。 今天1select * from 表名 where to_days(时间字段名)= to_days(now()); 昨天1select * from 表名 where datediff(字段,now())= -1; 本周1select name, submittime from enterprise where yearweek(date_format(submittime, '%Y-%m-%d'))= yearweek(now())- 1; 上周1select name, submittime from enterprise where yearweek(date_format(submittime, '%Y-%m-%d'))= yearweek(now())- 1; 近7天1select * from 表名 where date_sub(curdate(), interval 7 day)<= date(时间字段名); 近30天1select * from 表名 where date_sub(curdate(), interval 30 day)<= date(时间字段名); 本月1select * from 表名 where date_format(时间字段名,'%Y%m')= date_format(curdate(), '%Y%m'); 上一个月1select * from 表名 where period_diff(date_format(now(), '%Y%m'), date_format(时间字段名,'%Y%m'))= 1; 本季度1select * from 'ht_invoice_information' where quarter(create_date)=quarter(now()); 上季度1select * from 'ht_invoice-information' where quarter(create_date)=quarter(date_sub(now(), interval 1 quarter)); 本年1select * from 'ht_invoice_information' where year(create_date)=year(now()); 去年1select * from 'ht_invoice_information' where year(create_date)= year(date_sub(now(), interval 1 year)); Your support will encourage me to continue to create! Donate WeChat Pay Alipay