简体   繁体   English

mysql在两个日期/时间之间选择

[英]mysql select between two dates / times

I have a mysql db that has a date field as well as a time field. 我有一个mysql数据库,它有一个日期字段和一个时间字段。 I need to select the following argument from the db. 我需要从db中选择以下参数。

select from spiff where date is between 02/01/2016 and time is equal to or grater then 12:00:00 and date 02/08/2016 and time is equal too or less then 11:59:59 选择spiff,其中日期是在02/01/2016之间,时间等于或更大,然后是12:00:00和日期02/08/2016,时间等于或小于11:59:59

so in simple forum i want to select everything from 7 days ago till today from noon to noon where 12;00:00 is the cut off time. 所以在简单的论坛中,我想选择从7天前到今天中午到中午的所有内容,其中12; 00:00是截止时间。

I have this code but it doesnt do quite what i want 我有这个代码,但它并没有做我想要的

SELECT * 
FROM spiff 
WHERE `date` BETWEEN '" . $week_before . "' AND  '" . $today . "' AND time <= '12:00:00'  
ORDER by id DESC

That's not going to work. 那不行。 You can't store the date and time in separate fields, then search by both. 您不能将日期和时间存储在单独的字段中,然后按两者搜索。 You're excluding all times LATER than "noon", which means that if you have these dates/times: 您排除的时间超过“中午”,这意味着如果您有这些日期/时间:

02/02/2016 13:00    excluded
02/03/2016 10:00    included
02/04/2016 17:59    excluded

Even those the dates fall within your specified range, the 13:00 and 17:59 ones are EXCLUDED becomes they're NOT < '12:00:00' 即使那些日期落在你指定的范围内, 13:0017:59的日期也会被排除,因为它们不是< '12:00:00'

You need to store the dates as a single datetime field, and then your query becomes a trivial: 您需要将日期存储为单个日期时间字段,然后您的查询变得微不足道:

SELECT ... WHERE datetimefield BETWEEN '2016-02-01 12:00:00' AND '2016-02-08 11:59:59'

And note that for these sorts of date/time comparisons to work, you HAVE to be using mysql's actual date/time fields, and mysql's date-as-time string format: yyyy-mm-dd . 请注意,对于这些类型的日期/时间比较工作,您必须使用mysql的实际日期/时间字段,以及mysql的日期 - 时间字符串格式: yyyy-mm-dd If your dates are stored as mm/dd/yyyy , then they're NOT dates, they're strings, and mysql's string comparison rules apply, which means that 如果您的日期存储为mm/dd/yyyy ,那么它们不是日期,它们是字符串,并且mysql的字符串比较规则适用,这意味着

'10/11/2015' > '09/01/1970' -> TRUE

because 10 is larger than 09 . 因为10大于09

The problem is that you have the date and the time parts in separate fields. 问题是您在单独的字段中有日期和时间部分。 As a result of the AND time <= '12:00:00' condition in the where clause, MySQL will return records from the morning for all affected dates. 由于where子句中的AND time <= '12:00:00'条件,MySQL将从早上返回所有受影响日期的记录。

The best solution would be to combine the 2 fields into one with datetime data type. 最好的解决方案是将两个字段组合成一个具有datetime数据类型的字段。 This way the where clause would be: 这样where子句将是:

...`datime_field` BETWEEN '" . $week_before_noon . "' AND  '" . $today_noon . "'

The 2 php variables would contain a valid datetime expression in the format of YYYY-MM-DD hh:mm:ss 2个php变量将包含一个有效的日期时间表达式,格式为YYYY-MM-DD hh:mm:ss

The alternative solution is to combine the values from the 2 fields on the fly, but this way you cannot use indexes to speed up the search: 另一种解决方案是动态组合来自2个字段的值,但这样您就无法使用索引来加速搜索:

... date + time BETWEEN '" . $week_before . " 12:00:00' AND  '" . $today . " 12:00:00'

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

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