Rick Joe Rick Joe - 4 months ago 8
SQL Question

Get posts done today but at least 20 minutes older

I want to get the posts from today that I made, all of them, except the ones that I posted in the last 20 minutes.

for example, now is 3:30.

I want to get all posts that I did today until 3:10.

I tried:

SELECT id, titulo
FROM posts
WHERE data > DATE_SUB(NOW(), INTERVAL 20 MINUTE)
ORDER BY RAND()


but no success. what is wrong?

Answer

Your query is set to get posts from the last 20 minutes, if you want today except the last 20 minutes, you should use data < DATE_SUB( NOW( ) , INTERVAL 20 MINUTE ) and not bigger than.

The second part (from today) can be done with curdate() function:

CURDATE() = DATE(data)

and together:

SELECT id, titulo
FROM posts WHERE 
data < DATE_SUB( NOW( ) , INTERVAL 20 MINUTE ) 
AND
CURDATE() = DATE(data)
ORDER BY RAND( )