MySQL“之间”条款不包含在内?

发布于 2024-10-19 04:59:12 字数 356 浏览 4 评论 0原文

如果我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

荒芜了季节 2024-10-26 04:59:12

来自 MySQL 手册

这相当于表达式
(min <= expr AND expr <= max)

From the MySQL-manual:

This is equivalent to the expression
(min <= expr AND expr <= max)

醉殇 2024-10-26 04:59:12

字段dob可能有一个时间部分。

要将其截断:

select * from person 
where CAST(dob AS DATE) between '2011-01-01' and '2011-01-31'

The field dob probably has a time component.

To truncate it out:

select * from person 
where CAST(dob AS DATE) between '2011-01-01' and '2011-01-31'
羞稚 2024-10-26 04:59:12

问题是 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.

幽梦紫曦~ 2024-10-26 04:59:12
select * from person where dob between '2011-01-01 00:00:00' and '2011-01-31 23:59:59'
select * from person where dob between '2011-01-01 00:00:00' and '2011-01-31 23:59:59'
单身狗的梦 2024-10-26 04:59:12

您在查询中引用的字段是日期类型还是日期时间类型?

您所描述的行为的一个常见原因是当您使用 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.

酒解孤独 2024-10-26 04:59:12

您好,这个查询对我有用,

select * from person where dob between '2011-01-01' and '2011-01-31 23:59:59'

Hi this query works for me,

select * from person where dob between '2011-01-01' and '2011-01-31 23:59:59'
箹锭⒈辈孓 2024-10-26 04:59:12
select * from person where DATE(dob) between '2011-01-01' and '2011-01-31'

令人惊讶的是,这样的转换可以解决 MySQL 中的许多问题。

select * from person where DATE(dob) between '2011-01-01' and '2011-01-31'

Surprisingly such conversions are solutions to many problems in MySQL.

追风人 2024-10-26 04:59:12

在 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 upto 2011-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 upto 2011-01-31 23:59:59

虫児飞 2024-10-26 04:59:12

您可以运行查询:

select * from person where dob between '2011-01-01' and '2011-01-31 23:59:59'

就像其他人指出的那样,如果您的日期是硬编码的。

另一方面,如果日期在另一个表中,您可以添加一天并减去一秒(如果保存的日期没有秒/时间),例如:

select * from person JOIN some_table ... where dob between some_table.initial_date and (some_table.final_date + INTERVAL 1 DAY - INTERVAL 1 SECOND)

避免在 dob 上进行强制转换fiels(就像在接受的答案中一样),因为这可能会导致巨大的性能问题(比如无法在 dob 字段中使用索引,假设有一个)。如果您执行类似 DATE(dob)CAST(dob AS日期),所以要小心!

You can run the query as:

select * from person where dob between '2011-01-01' and '2011-01-31 23:59:59'

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:

select * from person JOIN some_table ... where dob between some_table.initial_date and (some_table.final_date + INTERVAL 1 DAY - INTERVAL 1 SECOND)

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 the dob field, assuming there is one). The execution plan may change from using index condition to using where if you make something like DATE(dob) or CAST(dob AS DATE), so be careful!

爱她像谁 2024-10-26 04:59:12

将上限日期设置为 date + 1 天,因此根据您的情况,将其设置为 2011-02-01。

Set the upper date to date + 1 day, so in your case, set it to 2011-02-01.

鱼忆七猫命九 2024-10-26 04:59:12
select * from person where dob between '2011-01-01' and '2011-01-31' or dob like' 2011-01-31%'

只需添加 或 <>如“日期%”

select * from person where dob between '2011-01-01' and '2011-01-31' or dob like' 2011-01-31%'

Just add or <<column>> like "date%".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文