SELECT * FROM 表名 WHERE TO_DAYS(时间字段名) = TO_DAYS(NOW());
SELECT * FROM 表名 WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段名) <= 1;
SELECT * FROM 表名 WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名);
SELECT * FROM 表名 WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名);
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_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(DATE_SUB(CURDATE(), INTERVAL 1 MONTH),'%Y-%m');
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));
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 上月最后一天 and 下月第一天;
SELECT DATE_FORMAT(FROM_UNIXTIME(时间字段名/1000),'%Y%m%d') from 表名;
SELECT * FROM 表名 WHERE DATE_FORMAT(FROM_UNIXTIME(时间字段名/1000),'%Y%m%d') = DATE_FORMAT(NOW(),'%Y%m%d');
SELECT UNIX_TIMESTAMP('2018-08-30 19-40-07')
如果参数为空,则处理为当前时间
mysql中使用UNIX_TIMESTAMP和FROM_UNIXTIME可以不用指定日期类型