备注:
如果时间为时间戳格式 以下时间字段改为:
FROM_UNIXTIME(时间字段名,"%Y-%m-%d %H:%i:%S")
今天
select * from 表名 where to_days(时间字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <&#61; 1
7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <&#61; date(时间字段名)
近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <&#61; date(时间字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, &#39;%Y%m&#39; ) &#61; DATE_FORMAT( CURDATE( ) , &#39;%Y%m&#39; )
上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , &#39;%Y%m&#39; ) ,
date_format( 时间字段名, &#39;%Y%m&#39; ) ) &#61;1
查询本季度数据
select * from &#96;ht_invoice_information&#96; where QUARTER(create_date)&#61;QUARTER(now());
查询上季度数据
select * from &#96;ht_invoice_information&#96; where QUARTER(create_date)&#61;QUARTER(DATE_SUB(now(),interval 1 QUARTER));
查询本年数据
select * from &#96;ht_invoice_information&#96; where YEAR(create_date)&#61;YEAR(NOW());
查询上年数据
select * from &#96;ht_invoice_information&#96; where year(create_date)&#61;year(date_sub(now(),interval 1 year));
查询当前这周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,&#39;%Y-%m-%d&#39;)) &#61; YEARWEEK(now());
查询上周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,&#39;%Y-%m-%d&#39;)) &#61; YEARWEEK(now())-1;
查询当前月份的数据
select name,submittime from enterprise where date_format(submittime,&#39;%Y-%m&#39;)&#61;date_format(now(),&#39;%Y-%m&#39;)
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询上个月的数据
select name,submittime from enterprise where
date_format(submittime,&#39;%Y-%m&#39;)&#61;date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),&#39;%Y-%m&#39;)
select * from &#96; user &#96; where DATE_FORMAT(pudate, &#39; %Y%m &#39; ) &#61; DATE_FORMAT(CURDATE(), &#39; %Y%m &#39; ) ;
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,&#39;%y-%m-%d&#39;)) &#61; WEEKOFYEAR(now())
select *
from user
where MONTH (FROM_UNIXTIME(pudate, &#39; %y-%m-%d &#39; )) &#61; MONTH (now())
select *
from [ user ]
where YEAR (FROM_UNIXTIME(pudate, &#39; %y-%m-%d &#39; )) &#61; YEAR (now())
and MONTH (FROM_UNIXTIME(pudate, &#39; %y-%m-%d &#39; )) &#61; MONTH (now())
select *
from [ user ]
where pudate between 上月最后一天
and 下月第一天
where date(regdate) &#61; curdate();
select * from test where year(regdate)&#61;year(now()) and month(regdate)&#61;month(now())
and day(regdate)&#61;day(now())
SELECT date( c_instime ) ,curdate( )
FROM &#96;t_score&#96;
WHERE 1
LIMIT 0 , 30