mysql query today, yesterday, this week, this month, last month, this year's data

Some of the mysql database query operations on the following dates:

 - Today 

the SELECT * from table name the WHERE TO_DAYS (time field name) = TO_DAYS (now ());

 - Yesterday 

the SELECT * the FROM table WHERE TO_DAYS (NOW ()) - TO_DAYS (time field) <= . 1
 
- week 

the SELECT * the FROM table name WHERE yEARWEEK (date_format (time field name, ' % Y-M-% D% ' )) = yearWeek (now ());

 - month 

SELECT * the FROM table name WHERE DATE_FORMAT (time field name, ' %% the Y m ' ) = the DATE_FORMAT (CURDATE (), ' %% the Y m ' )

 - one month on 

SELECT* FROM table name PERIOD_DIFF the WHERE (DATE_FORMAT (now (), ' %% the Y m ' ), DATE_FORMAT (Time field name, ' %% the Y m ' ) = . 1
 
- year 

the SELECT * FROM table name WHERE YEAR (Time field name) = YEAR (the nOW ())


 - on January 

the SELECT * the FROM table name PERIOD_DIFF the WHERE (DATE_FORMAT (now (), ' %% the Y m ' ), DATE_FORMAT (time field name, ' %% the Y m ' )) = 1

 
 
- query data for the quarter
 the SELECT * from `ht_invoice_information` the WHERE qUARTER (create_date) = qUARTER (now ());
 - a query on quarterly data
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
--查询本年数据
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
--查询上年数据
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

 


--查询当前这周的数据 
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) =YearWeek (now ());

 - Query data week 
the SELECT name, the FROM Enterprise submittime the WHERE yearWeek (DATE_FORMAT (submittime, ' % Y-M-% D% ' )) = yearWeek (now ()) - . 1 ;

 - - query the current month data
 SELECT name, submittime from Enterprise WHERE DATE_FORMAT (submittime, ' %% Y-m ' ) = DATE_FORMAT (now (), ' %% Y-m ' )

 - query from this now six months data
 SELECT name, submittime from Enterprise WHERE submittime BETWEEN DATE_SUB (now (), interval The . 6 month The) and now ();

 -查询上个月的数据
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')

select * from ` user ` where DATE_FORMAT(pudate, ' %Y%m ' ) = DATE_FORMAT(CURDATE(), ' %Y%m ' ) ;

select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())

select * 
from user 
where MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now())

select * 
from [ user ] 
where YEAR (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = YEAR (now())
and MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now())

select * 
from [ user ] 
where pudate between 上月最后一天
and 下月第一天

where date(regdate) = curdate();

select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())

SELECT date( c_instime ) ,curdate( )
FROM `t_score`
WHERE 1
LIMIT 0 , 30

Transfer: https://www.cnblogs.com/shuilangyizu/p/8805384.html

Guess you like

Origin www.cnblogs.com/Testcase/p/11404627.html
Recommended