MySql time operation

1. Check the date of the day

select current_date();

 

2. Check the time of day

select current_time();

 

3. Check the date and time of the day

select current_timestamp();

 

4. Query the record of the day

select * from table name where to_days(time field name) = to_days(now());

 

5. Query yesterday's records

SELECT * FROM table name WHERE TO_DAYS( NOW( ) ) – TO_DAYS(time field name) <= 1

 

6. Query 7 days of records

SELECT * FROM table name where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(time field name) 

 

7. Query records of the past 30 days

SELECT * FROM table name where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(time field name)

 

8. Query the records of this month

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

 

9. Query the records of the previous month

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

 

10. Query the data of this quarter

select * from 表名 where QUARTER(create_date)=QUARTER(now());


11. Query the data of the last quarter
select * from table name where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

 

12. Query this year's data
select * from table name where YEAR(create_date)=YEAR(NOW());

 

13. Query the data of the previous year
select * from table name where year(create_date)=year(date_sub(now(),interval 1 year)); 14. Query the data of the current week  SELECT * FROM table name WHERE YEARWEEK(date_format(submittime ,'%Y-%m-%d')) = YEARWEEK(now());
 

 

15. Query last week's data
SELECT * FROM table name WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

 

16. Query the data of the current month
select * from table name where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

 

17. Query data 6 months away from the current
select name, submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614268&siteId=291194637