69

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?

Matthew Lock
  • 12,582
  • 11
  • 88
  • 124
Pieter888
  • 4,684
  • 12
  • 52
  • 74

4 Answers4

189
select * 
from orders 
where order_date >= DATE_SUB(NOW(),INTERVAL 1 YEAR);
Yogesh Suthar
  • 30,136
  • 18
  • 69
  • 98
nos
  • 215,098
  • 54
  • 400
  • 488
  • 2
    I think curdate() also can be used inplace of NOW() – Developer Feb 17 '11 at 10:33
  • @Dotnet , hi Dotdot , Do you know order_date is of what Datatype?? It can also be DATE TIME Field so , Why take risk ? You wont have to modify code though you change its datatype to DATE TIME . Getting ? – Pratik Apr 20 '15 at 07:31
11
SELECT * FROM order WHERE order_date >= curdate() - interval 1 year;
Dimanenator I
  • 111
  • 1
  • 4
1

To first of month a year ago

SELECT DATE_SUB(DATE_FORMAT(CURRENT_DATE,'%Y-%m-01'),INTERVAL 1 YEAR);
zzapper
  • 4,455
  • 5
  • 47
  • 44
-5

I hope it helps you:

select * 
from table 
where (order_date BETWEEN '2/15/2011 3:36:18 PM' AND '2/17/2011 9:00:00 PM')
Yury Fedorov
  • 13,640
  • 6
  • 50
  • 65
  • 3
    this will work only for particular date but how will you check for the change in day or year each and every time you can not change the date right – Developer Feb 17 '11 at 10:37