73

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
  • 13,144
  • 12
  • 92
  • 130
Pieter888
  • 4,882
  • 13
  • 53
  • 74

4 Answers4

198
select * 
from orders 
where order_date >= DATE_SUB(NOW(),INTERVAL 1 YEAR);
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
nos
  • 223,662
  • 58
  • 417
  • 506
  • 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 Joshi Apr 20 '15 at 07:31
12
SELECT * FROM order WHERE order_date >= curdate() - interval 1 year;
Dimanenator I
  • 121
  • 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,743
  • 5
  • 48
  • 45
-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
  • 14,508
  • 6
  • 50
  • 66
  • 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