Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Current date

Tags:

sql

mysql

I would like to have user can see their today picture uploaded on profile page.

Is this correct?

SELECT * FROM pictures 
WHERE userid = '$userid'
ORDER BY pictureuploaddate < DATE_ADD(NOW(), INTERVAL 1 DAY);

Still not working. Thanks for the help.

like image 787
user2279205 Avatar asked Aug 30 '26 09:08

user2279205


2 Answers

You would get something like:

SELECT * 
FROM pictures 
WHERE userid = '$userid' AND 
      DATE(pictureuploaddate) = CURDATE() # Match date without time
ORDER BY pictureuploaddate DESC

Why do you ORDER BY and use an = in it? It should be todays date, just add it to the WHERE. If you want to get the latest picture first you can ORDER BY pictureuploaddate DESC

Also it's better to compare dates instead of smaller than if you want current date. Because it is faster to match.

like image 177
Niels Avatar answered Aug 31 '26 23:08

Niels


I know you picked an answer already, but to avoid any confusion with multiple uploads on the same day, you could have also done:

SELECT * 
FROM pictures 
WHERE userid = '$userid'
ORDER BY pictureuploaddate DESC
LIMIT 1;
like image 44
Cargo23 Avatar answered Aug 31 '26 22:08

Cargo23