Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql query to select records with a particular date

Tags:

datetime

mysql

so right now I'm storing a the date of each search done on my site like this

2011-06-07 21:44:01

now I'd like to execute a query to select all values where the date is equal to whatever, but only the year / month and day are taken into account, so it would be like

mysql_query("SELECT tag from tags WHERE date = '2011-06-07'");

but it shouldn't take into account the exact time (hour minute seconds), is there any way to do this?

like image 712
Belgin Fish Avatar asked Jun 08 '11 01:06

Belgin Fish


3 Answers

You could use the DATE() function.

SELECT `tag`
  FROM `tags`
 WHERE DATE(`date`) = '2011-06-07'

However, for better performance you could use...

  WHERE `date` 
BETWEEN '2011-06-07'
    AND '2011-06-07 23:59:59'
like image 167
alex Avatar answered Oct 17 '22 10:10

alex


This should work:

mysql_query("SELECT tag from tags WHERE date LIKE '2011-06-07%'");
like image 23
Sparkup Avatar answered Oct 17 '22 12:10

Sparkup


mysql_query("SELECT tag from tags WHERE DATE(`date`) = '2011-06-07'");

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date

like image 1
Phphelp Avatar answered Oct 17 '22 10:10

Phphelp