/  Technology   /  Get Last 15 Days Records in MySQL

Get Last 15 Days Records in MySQL

 

In some cases, you may need to retrieve data from the last 15 days in MySQL. Here is how to retrieve records for the last 15 days in MySQL. It can also be used to obtain the number of new users signed up in the last 15 days, or to select last 15 days’ sales data for analysis.

Here are the steps to get the last 15 days’ records in MySQL.

The following table orders(order_date, sale, orders) contains the daily number of orders and sale amount.

Example:

 

1.Create table

#start
create table orders(order_date date,sale int, orders int);
#end

2. Insert data into Table

#start
insert into orders(order_date ,sale ,orders )
values( '2023-01-17' , 300 , 11 ),
( '2023-01-18' , 250 , 25 ),
( '2023-01-19' , 250 , 22 ),
( '2023-01-20' , 250 , 24 ),
( '2023-01-21' , 150 , 30 ),
( '2023-01-22' , 300 , 11 ),
( '2023-01-23' , 200 , 35 ),
( '2023-01-24' , 200 , 27 ),
( '2023-01-25' , 250 , 22 ),
( '2023-01-26' , 150 , 25 ),
( '2023-01-27' , 300 , 22 ),
( '2023-01-28' , 280 , 28 ),
( '2023-01-29' , 320 , 26 ),
( '2023-01-30' , 400 , 25 ),
( '2023-01-31' , 400 , 25 ),
( '2023-02-01' , 250 , 23 ),
( '2023-02-02' , 100 , 26 ),
( '2023-02-03' , 200 , 38 );
#end

3. Select Table

#start
select * from orders;
#end

How to Get Last 15 Days Sales Data in SQL

The following SQL query will retrieve records for the last 15 days in MySQL

#start
select * from orders
where order_date> now() - INTERVAL 15 day;
#end

Output:

In the above query, we use the system function now() to obtain the current date and time. We then use the INTERVAL clause to filter records where the order_date falls after 15 days prior to the present date.

 

Leave a comment