tags:

views:

120

answers:

2

I was playing with MYSQL and I know there's a limit command that shows a certain amount of results, but i was wondering if MySQL alone can show only the last 3 days or something. Just wondering.

Update: I used NOW() to store times.

A: 

You could use a combination of the UNIX_TIMESTAMP() function to do that.

SELECT ... FROM ... WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(thefield) < 259200
Turnor
@Turnor, caution, days do not always consist of 86,400 seconds, what with leap seconds and daylight savings.
pilcrow
True. But it's rare that you'd need that level of precision, and it didn't sound like the original question did.
Turnor
+3  A: 

Use for a date three days ago:

WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -3 DAY);

Check the DATE_ADD documentation.

Or you can use:

WHERE t.date >= ( CURDATE() - INTERVAL 3 DAY )
OMG Ponies
"WHERE t.date >= ( CURDATE() - INTERVAL 3 DAY )" should be sufficient
Salman A
VERY INTERESTING! I tried running that with DATE_ADD and it kept failing because of syntax. However, it worked without DATE_ADD, so I only used `CURDATE() - INTERVAL 3 DAY`. Why is that?
Doug
I was running it through phpmyadmin if that makes a difference.
Doug