Query Mysql statements in recent days and weeks

First understand the date function of adding/subtracting days and weeks to mysql date

MySQL adds a time interval to a date: date_add()

now() //now function is to get the current time

select date_add(now(), interval 1 day); - 加1天

select date_add( now() , interval 1 hour); - add 1 hour

select date_add( now() , interval 1 minute); - add 1 minute

select date_add(now(), interval 1 second); -加1秒

select date_add( now() , interval 1 microsecond); - add 1 millisecond

select date_add(now(), interval 1 week);-加1周

select date_add(now(), interval 1 month);-加1月

select date_add(now(), interval 1 quarter);-加1季

select date_add(now(), interval 1 year);-加1年

MySQL adddate(), addtime() functions can be replaced by date_add().

2. MySQL subtracts a time interval from a date: date_sub()

The MySQL date_sub() datetime function is used in the same way as date_add().

The subdate() and subtime() functions in MySQL are recommended to be replaced by date_sub().


For example, I have a table of sales records with a sales date field (xsrq), if I want to query the sales records for the last week

SELECT * FROM XSJL WHERE XSRQ >= DATE_ADD(NOW(),INTERVAL -7 day);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326464535&siteId=291194637