当前位置:网站首页>How to query the data of a certain day, a certain month, and a certain year in MySQL

How to query the data of a certain day, a certain month, and a certain year in MySQL

2022-07-07 16:12:00 aGreetSmile

 MySQL in , How to query a day , Some month , Data for a certain year

 today 

select * from  Table name  where to_days( Time field name ) = to_days(now());
 yesterday 

SELECT * FROM  Table name  WHERE TO_DAYS( NOW( ) ) - TO_DAYS(  Time field name ) <= 1
 near 7 God 

SELECT * FROM  Table name  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date( Time field name )
 near 30 God 

SELECT * FROM  Table name  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date( Time field name )
 This month, 

SELECT * FROM  Table name  WHERE DATE_FORMAT(  Time field name , '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
 Last month 

SELECT * FROM  Table name  WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format(  Time field name , '%Y%m' ) ) =1
 Query this quarter's data 

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
 Query last quarter data 

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
 Query this year's data 

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
 Query the data of last year 

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
 Query the current week's data 

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
 Look up last week's data 

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
 Look up last month's data 

 Copy code 
 Copy code 
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   Last day of last month   and  The first day of next month  
 Copy code 
 Copy code 
 Query the data of the current month  

select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
 Query distance now 6 Months of data 

 

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
 

  Query the data of a month ( Inquire about 17 year 10 Monthly data )

select * from exam   where date_format(starttime,'%Y-%m')='2017-10'
 

select * from exam   where date_format(starttime,'%Y-%m')=date_format('2017-10-05','%Y-%m')

原网站

版权声明
本文为[aGreetSmile]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071402018660.html