在日常开发中,可能会存在数据筛选上需要通过时间上的筛选,我们通过between and就能解决了,不过如果以本周、本月这种还得去计算时间段,没有必要浪费时间,那我们通过SQL的函数就可以解决问题
1、查询今天
SELECT * FROM your_table WHERE DATE(date_column) = CURDATE();
2、查询本周
SELECT * FROM your_table WHERE YEARWEEK(date_column) = YEARWEEK(CURDATE());
3、查询本月
SELECT * FROM your_table WHERE MONTH(date_column) = MONTH(CURDATE()) AND YEAR(date_column) = YEAR(CURDATE());
4、查询近三个月
SELECT * FROM your_table WHERE QUARTER(date_column) = QUARTER(CURDATE());
5、查询近半年
SELECT * FROM your_table WHERE TIMESTAMPDIFF(MONTH, date_column, CURDATE()) <= 6;
6、查询近一年?
SELECT * FROM your_table WHERE TIMESTAMPDIFF(YEAR, date_column, CURDATE()) <= 1;
7、查询近三年
SELECT * FROM your_table WHERE TIMESTAMPDIFF(YEAR, date_column, CURDATE()) <= 3;