赞
踩
MySQL获取某个时间范围内的数据 TO_DAYS(date)函数。
to_days():返回从0000年至当前日期的总天数。
目录
表数据:
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_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 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 YEARWEEK(date_format(字段名,'%Y-%m-%d')) = YEARWEEK(now());
SELECT * FROM 表名 WHERE YEARWEEK(date_format(字段名,'%Y-%m-%d')) = YEARWEEK(now())-1;
为空是因为默认按周日为第一天。
根据yearweek(date,mode)可知,第二个参数可以设置第一天是周几。
select * from 表名 where date_format(字段名,'%Y-%m')=date_format(now(),'%Y-%m')
select * from 表名 where 字段名 between date_sub(now(),interval 6 month) and now();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。