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

在SQL中,BETWEEN子句用于选取介于两个值之间的记录,但默认不包含上限值。文章指出,当日期字段包含时间组件时,可能会导致意外地排除掉上限日期。解决方案是通过将日期字段转换为日期类型,如使用CAST函数,确保查询包含整个日期范围。这有助于避免由于时间部分引起的不期望的排除问题。

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.)

解决方案

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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值