MySQL“之间”条款不包含在内?
如果我使用 Between 子句运行查询,它似乎排除了结束值。
例如:
select * from person where dob between '2011-01-01' and '2011-01-31'
这将获取从“2011-01-01”到“2011-01-30”的所有带有 dob
的结果;跳过 dob
为“2011-01-31”的记录。谁能解释为什么这个查询会这样,以及我如何修改它以包含 dob
为“2011-01-31”的记录? (结束日期无需加 1,因为它是由用户选择的。)
If I run a query with a between
clause, it seems to exclude the ending value.
For example:
select * from person where dob between '2011-01-01' and '2011-01-31'
This gets all results with dob
from '2011-01-01' till '2011-01-30'; skipping records where dob
is '2011-01-31'. Can anyone explain why this query behaves this way, and how I could modify it to include records where dob
is '2011-01-31'? (without adding 1 to the ending date because its been selected by the users.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
来自 MySQL 手册:
From the MySQL-manual:
字段
dob
可能有一个时间部分。要将其截断:
The field
dob
probably has a time component.To truncate it out:
问题是 2011-01-31 实际上是 2011-01-31 00:00:00。这就是一天的开始。白天的一切都不包括在内。
The problem is that 2011-01-31 really is 2011-01-31 00:00:00. That is the beginning of the day. Everything during the day is not included.
您在查询中引用的字段是日期类型还是日期时间类型?
您所描述的行为的一个常见原因是当您使用 DateTime 类型时,您实际上应该使用 Date 类型。也就是说,除非您确实需要知道某人的出生时间,否则只需使用 Date 类型即可。
最后一天未包含在结果中的原因是查询假定您未在查询中指定的日期的时间部分。
也就是说:您的查询被解释为截至 2011 年 1 月 30 日至 2011 年 1 月 31 日之间的午夜,但数据可能在 2011 年 1 月 31 日晚些时候的某个时间具有值。
建议:如果是DateTime类型,请将字段更改为Date类型。
Is the field you are referencing in your query a Date type or a DateTime type?
A common cause of the behavior you describe is when you use a DateTime type where you really should be using a Date type. That is, unless you really need to know what time someone was born, just use the Date type.
The reason the final day is not being included in your results is the way that the query is assuming the time portion of the dates that you did not specify in your query.
That is: Your query is being interpreted as up to Midnight between 2011-01-30 and 2011-01-31, but the data may have a value sometime later in the day on 2011-01-31.
Suggestion: Change the field to the Date type if it is a DateTime type.
您好,这个查询对我有用,
Hi this query works for me,
令人惊讶的是,这样的转换可以解决 MySQL 中的许多问题。
Surprisingly such conversions are solutions to many problems in MySQL.
在 MySql 中,值之间包含在内,因此当您尝试获取“2011-01-01”和“2011-01-31”之间的值时,
它将包含
2011-01-01 00:00:00
截至2011-01-31 00:00:00
因此 2011-01-31 实际上没有任何内容,因为它的时间应该从
2011-01-31 00:00:00 ~ 2011-01-31 23:59:59
对于上限,您可以更改到
2011-02-01
那么它将获取截至2011-01-31 23:59:59
的所有数据In MySql between the values are inclusive therefore when you give try to get between '2011-01-01' and '2011-01-31'
it will include from
2011-01-01 00:00:00
upto2011-01-31 00:00:00
therefore nothing actually in 2011-01-31 since its time should go from
2011-01-31 00:00:00 ~ 2011-01-31 23:59:59
For the upper bound you can change to
2011-02-01
then it will get all data upto2011-01-31 23:59:59
您可以运行查询:
就像其他人指出的那样,如果您的日期是硬编码的。
另一方面,如果日期在另一个表中,您可以添加一天并减去一秒(如果保存的日期没有秒/时间),例如:
避免在
dob
上进行强制转换fiels(就像在接受的答案中一样),因为这可能会导致巨大的性能问题(比如无法在dob
字段中使用索引,假设有一个)。如果您执行类似DATE(dob)
或CAST(dob AS日期)
,所以要小心!You can run the query as:
like others pointed out, if your dates are hardcoded.
On the other hand, if the date is in another table, you can add a day and subtract a second (if the dates are saved without the second/time), like:
Avoid doing casts on the
dob
fiels (like in the accepted answer), because that can cause huge performance problems (like not being able to use an index in thedob
field, assuming there is one). The execution plan may change fromusing index condition
tousing where
if you make something likeDATE(dob)
orCAST(dob AS DATE)
, so be careful!将上限日期设置为 date + 1 天,因此根据您的情况,将其设置为 2011-02-01。
Set the upper date to date + 1 day, so in your case, set it to 2011-02-01.
只需添加
或 <>如“日期%”
。Just add
or <<column>> like "date%"
.