MySql query data within a certain period of time

1. Use the function DATE_SUB

The DATE_SUB function subtracts a certain time from the current time

DATE_SUB(date,INTERVAL expr unit)

date: legal date, which can be the current time now()

expr: the time you want to subtract, an integer

unit: the unit for subtracting time, such as day, week, month, year

example

Data from a week ago

select * from u_user where u_create_time > DATE_SUB(NOW(),INTERVAL 1 WEEK);

2. Use the function DATE_ADD

The DATE_ADD function adds a certain time to the current time

DATE_ADD(date,INTERVAL expr unitda

date: legal date, which can be the current time now(), or a time field in the table

expr: the time you want to add, integer

unit: add the unit of time, such as day, week, month, year

Example:

Data from three months ago

select * from u_user where DATE_ADD(u_create_time,INTERVAL 3 MONTH) > NOW();

Guess you like

Origin blog.csdn.net/CharlesYooSky/article/details/125763452