简体   繁体   中英

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

I want to extract customers created on 2021-10-14 (yyyy-mm-dd),

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

This is giving 0 rows as output. Whereas this code is showing the data created on 2021-10-14:

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

why is equal to not working in the above query and why is greater than including the given date.

Probably because your created_at column is of type datetime . And for instance a created time of

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

is not not the same as

'2021-10-14'

because it is actually comparing the time value too which is

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

The query you used (with the fixed typo of >= ) is actually the correct way to go since it is able to use indexes for better performance

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM