MySql 查询以选择具有特定日期的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6273361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:09:57  来源:igfitidea点击:

MySql query to select records with a particular date

mysqldatetime

提问by Belgin Fish

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?

但它不应该考虑确切的时间(小时分秒),有没有办法做到这一点?

回答by alex

You could use the DATE()function.

您可以使用该DATE()功能。

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'

回答by Sparkup

This should work:

这应该有效:

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

回答by Eric Kirima

I know the answer am about to post is long overdue but it can help someone who may experience the same problem.

我知道即将发布的答案姗姗来迟,但它可以帮助可能遇到同样问题的人。

For my case I used:

对于我的情况,我使用了:

SELECT * 
FROM tablename 
WHERE dateCol >= date("2017-11-01") 
AND dateCol < date("2017-11-01") + INTERVAL 1 DAY;

Its faster than using DATE() function.

它比使用 DATE() 函数更快。

If you are using a stored procedure, you could pass in the date as a string and supply it as an argument in place of ("2017-11-01")

如果您使用的是存储过程,则可以将日期作为字符串传入并将其作为参数提供来代替("2017-11-01")