Skip to content
Advertisement

Select by -3 days?

I’m trying to select data from a specific table in my database, but I want to be able to only view the last 3 days worth of data, I have the following code but for some reason I can’t get it to work.

SELECT * FROM demands WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -3 DAY)

Advertisement

Answer

You may avoid usage of DATE_ADD() at all:

SELECT * FROM demands as t WHERE t.date >= (CURDATE() - INTERVAL 3 DAY)

As @OGHaza mentioned, you specified column with alias to nowhere: t.date should be just date (note that it is a reserved word, so you should use backticks around it in this case) or demands should be specified with an alias like demands as t.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement