MySql查询7天,1个月,半年等数据

表名:tableName 字段名:myDate 最近7天的,
SELECT * FROM tableName where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(myDate);
最近一个月的
SELECT * FROM tableName where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(myDate);
最近1年的
SELECT * FROM tableName where DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= date(myDate);
本周的 (是从周日开始)
SELECT * FROM tableName where YEARWEEK(DATE_FORMAT(myDate,'%Y-%m-%d')) = YEARWEEK(now());
上周的
SELECT * FROM tableName where YEARWEEK(DATE_FORMAT(myDate,'%Y-%m-%d')) = YEARWEEK(now())-1;
本月的
SELECT * FROM tableName where  date_format(myDate,'%Y-%m')=date_format(now(),'%Y-%m')
上个月的
SELECT * FROM tableName where date_format(myDate,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m');
距离现在6个月的数据
SELECT * FROM tableName where  myDate between date_sub(now(),interval 6 month) and now();

你可能感兴趣的:(sql,mysql)