jerney
jerney

Reputation: 2243

SQL: Query for date greater than X months and Y days ago

Is it possible to construct a SQL query for MySQL that can SELECT based on a date being greater than 1 month and 4 days ago?

I know that the following is possible:

SELECT * FROM TBL WHERE DATE_COL > date_sub(now(), INTERVAL 1 MONTH);

However, what if I wanted to add another 4 days (or any number of days for that matter) to the interval in the date_sub?

Upvotes: 0

Views: 1920

Answers (2)

Chris
Chris

Reputation: 120

You should be able to wrap it:

SELECT * FROM TBL 
WHERE DATE_COL > date_sub(date_sub(now(), INTERVAL 1 MONTH), INTERVAL 4 DAY);

Upvotes: 5

ScaisEdge
ScaisEdge

Reputation: 133380

You could do with a nested date_sub

  SELECT * 
  FROM TBL 
  WHERE DATE_COL > date_sub(date_sub(now(), INTERVAL 1 MONTH), INTERVAL 4 DAY);

Upvotes: 0

Related Questions