为什么等号在 SQL 中不起作用,因为使用大于和小于来提取相同的日期

问题描述 投票:0回答:1

我想提取 2021-10-14 (yyyy-mm-dd) 创建的客户,

select * from customers where created_at = '2021-10-14'

这给出 0 行作为输出。 而此代码显示的是 2021 年 10 月 14 日创建的数据:

select * from customers where created_at > '2021-10-14' and created_at < '2021-10-15'

why 等于在上述查询中不起作用,why 大于包含给定日期。

sql mysql equality
1个回答
2
投票

可能是因为您的

created_at
列的类型为
datetime
。例如,创建时间为

'2021-10-14 12:00:00' 

不等于

'2021-10-14'

因为它实际上也在比较时间值,即

'2021-10-14 00:00:00' 

您使用的查询(带有固定拼写错误

>=
)实际上是正确的方法,因为它能够使用索引来获得更好的性能

select * 
from customers 
where created_at >= '2021-10-14' 
  and created_at < '2021-10-15'
© www.soinside.com 2019 - 2024. All rights reserved.