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

Why is the equal sign not working in SQL, where as same dates are being extracted with greater than and less than

我想提取在 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'

为什么等于在上面的查询中不起作用,为什么大于包含给定日期。

可能是因为您的 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'