Aamir Mukaram
Aamir Mukaram

Reputation: 49

Data from current date to 30 days after current days

Why this query is not working

SELECT * FROM history WHERE DATE(date) < CURDATE() + 30

I am trying to get the data from 30 days but my query is not working.Why

Upvotes: 0

Views: 1111

Answers (3)

hd1
hd1

Reputation: 34657

Because you're not using the right construct, try:

SELECT * FROM history WHERE DATE_ADD(date, INTERVAL 30 DAY);

Upvotes: 0

VMai
VMai

Reputation: 10336

To get the data from today on to 30 days after current day, you've got to set an upper and an lower limit, so use:

SELECT * FROM history WHERE 
    date >= CURDATE()
AND
    date < CURDATE() + INTERVAL 31 DAY

Please note that by not using a function on your date column you won't prohibit MySQL to use an index on this column.

The lower limit should be obvious, the upper limit means that you've got the complete day that's 30 days later than today. If you use + INTERVAL 30 DAY instead this last day is excluded from the result.

Upvotes: 0

John Conde
John Conde

Reputation: 219814

What does +30 mean? Days? Years? Months? Hours? You need to use (the proper syntax) a format MySQL understands:

SELECT * FROM history WHERE DATE(date) < CURDATE() + INTERVAL 30 DAY

Upvotes: 2

Related Questions