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
MySql query to select records with a particular date
提问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
回答by Sparkup
This should work:
这应该有效:
mysql_query("SELECT tag from tags WHERE date LIKE '2011-06-07%'");
回答by Phphelp
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
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date
回答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")