BradM
BradM

Reputation: 656

MySQL Date and Interval - records between dates

I've got a syntax problem I can't sort out. I'm just trying to grab all records from last 3 days.

$result = mysqli_query($link,"SELECT * FROM records WHERE today BETWEEN CURRENT_DATE AND DATE_ADD(CURRENT_DATE, INTERVAL 3 DAY)");

today is DB column for the MySQL timestamp and looks like this: 2014-10-30 16:35:58

This query only gives results for 1 day, not 3. Can someone help with the syntax problem?

Upvotes: 1

Views: 126

Answers (1)

Damian Yerrick
Damian Yerrick

Reputation: 4664

DATE_ADD(CURRENT_DATE, INTERVAL 3 DAY) means three days in the future, not three days ago. Unless the today column is supposed to represent (say) the date for which a future appointment is scheduled, you usually want to subtract days from a date. So get three days ago, you need to use DATE_SUB. I'd recommend this query:

SELECT *
FROM records
WHERE today >= DATE_SUB(CURRENT_DATE, INTERVAL 3 DAY)

Upvotes: 4

Related Questions