Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting all records from one year ago till now

Tags:

mysql

I have a table filled with a lot of rows and I need to select all the rows that are less than a year old till now.

The table (called orders) has a DateTime column named order_date, that's the field that determines when the order was placed.

How can I select all the records that have an order_date between now and a full year ago?

like image 456
Pieter888 Avatar asked Feb 17 '11 10:02

Pieter888


People also ask

How do I get last 6 months records in SQL?

Instead of approximating the "current" date by selecting the MAX(date) the code could reference CAST(GETDATE() as DATE) to access the system datetime and cast it as type DATE. where [date] > dateadd(month, -6, cast(getdate() as date));

How do I get last 30 days records in SQL?

SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).

How do I get last 7 days record in SQL?

Here's the SQL query to get records from last 7 days in MySQL. In the above query we select those records where order_date falls after a past interval of 7 days. We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.


1 Answers

select *  from orders  where order_date >= DATE_SUB(NOW(),INTERVAL 1 YEAR); 
like image 82
nos Avatar answered Oct 12 '22 01:10

nos