简体   繁体   English

MySQL日期搜索MM / DD / YYYY到MM / DD / YYYY

[英]MySQL Date Search MM/DD/YYYY to MM/DD/YYYY

I have a table in MySQL That looks like the following: 我在MySQL中有一个表,如下所示:

date        |storenum   |views 
-------------------------------
08/21/2009  |42         |3  
-------------------------------
08/22/2009  |43         |1    
-------------------------------
08/21/2009  |43         |4  
-------------------------------
08/22/2009  |42         |22
-------------------------------

I know that this is an unconventional date format, but I am using it as opposed to YYYY-MM-DD for ease of use. 我知道这是一种非常规的日期格式,但是为了易于使用,我使用它而不是YYYY-MM-DD。 Yet i have to search this database to get all "View Records" Between two dates. 但是我必须搜索该数据库以获取两个日期之间的所有“查看记录”。 In general it works but when I search from a date such as 01/01/2009 to 01/01/2010 I get no results. 一般来说,它可以工作,但是当我从2009年1月1日到2010年1月1日搜索时,没有任何结果。

Does anyone have any suggestions? 有没有人有什么建议? Constructive criticism is welcome. 欢迎进行建设性的批评。 Thanks! 谢谢!

In all honesty, change the way you store dates. 老实说,更改日期存储方式。 But for working with what you have now, you could look into using STR_TO_DATE() to convert these to dates that MySQL can work with. 但是,要使用现有功能,您可以考虑使用STR_TO_DATE()将它们转换为MySQL可以使用的日期。

You mention ease of use - I think the case you are trying to resolve now is an example of "not so easy to use" :). 您提到易用性-我认为您现在要解决的情况是“不太易用”的一个示例:)。

I would stick to the default format of dates in MySQL 我会坚持使用MySQL中日期的默认格式

yyyy-mm-dd h:i:s //php's date formatting 

and use DATE fields. 并使用DATE字段。 This will solve the searching/inserting of dates problem. 这将解决日期搜索/插入问题。 Though you need to change some code to fix displaying the dates. 尽管您需要更改一些代码来修复显示日期。

what is your date field type? 您的date字段类型是什么? for a correct use it should be date or datetime , and seeing what you have it looks like you store you dates in a varchar. 为了正确使用,应该将它设置为datedatetime ,然后看看所拥有的内容就像将dates存储在varchar中一样。 am I right? 我对吗?

This also have the advantage you can add indexes, and then search in ranges trough mysql 这也具有可以添加索引,然后在mysql范围内搜索的优势

my suggestion is get whatever the user enter. 我的建议是获取用户输入的任何内容。 and then convert it to a mysql date format for searching 然后将其转换为mysql日期格式以进行搜索

something like: 就像是:

<?php

$mysql_time = date('Y-m-d H:m:s' strotime('05/11/2008'));

$query = sprintf("SELECT * FROM sometable < '%s'", $mysql_time);

?>

尝试使用“ yyyyMMdd”格式的搜索,MySql肯定会识别它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM