Mysql实现查询当天、一周、一月,一年的数据

查询当天的数据

方法一:

SET @dt = CURDATE();
SELECT * FROM table_name WHERE condition1 AND conditions2 AND column_name=@dt;

方法二:

/*使用函数*/
SELECT * FROM table_name WHERE condition1 AND condition2 AND date(column_name)= CURDATE();

方法三:

SELECT * FROM table_name WHERE condition1 AND condition2 AND TO_DAYS(column_name)= TO_DAYS(NOW());

查询一周的数据

SELECT * FROM table_name WHERE condition1 AND condition2 AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_name);

方法二:

SELECT * FROM table_name WHERE condition1 AND condition2 AND DATE_SUB(CURDATE(), INTERVAL 1 WEEK) <= date(column_name);

查询一月的数据

SELECT * FROM table_name WHERE condition1 AND condition2 AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(column_name);

查询一年的数据

SELECT * FROM table_name WHERE condition1 AND condition2 AND DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= date(column_name);

 

你可能感兴趣的:(Mysql实现查询当天、一周、一月,一年的数据)