gpt4 book ai didi

sql - 这些日期 where 子句有什么区别?

转载 作者:行者123 更新时间:2023-12-03 22:56:28 25 4
gpt4 key购买 nike

这两个查询返回不同的订单计数,原因尚不完全清楚。第一个where条款是被接受的 correct版本:

where
year(OrderDate) = 2011
and MONTH(OrderDate) = 8
and DAY(OrderDate) = 3

但如果我说一些非常相似的话:
WHERE 
io.OrderDate >= '2011-08-03 00:00:00'
and io.OrderDate <= '2011-08-03 11:59:59'

我得到了完全不同的记录数。我在日期格式上做错了什么还是我在逻辑上遗漏了什么?我更愿意说后者,因为使用恕我直言更容易一些。

最佳答案

编辑:

我推荐的新答案结合了我的原始答案(如下所示)和 Mikael 和 Alex 的评论。

WHERE 
io.OrderDate >= '2011-08-03 00:00:00'
and io.OrderDate < '2011-08-04 00:00:00'

应该提供所需的结果。

第二个查询仅从午夜查找到中午。 (00:00:00 - 11:59:59 为十二小时)
WHERE 
io.OrderDate >= '2011-08-03 00:00:00'
and io.OrderDate <= '2011-08-03 23:59:59'

应该修复它。

@Alex 的回答也有道理。毫秒计数(取决于您的特定表是否包含它们)。
WHERE 
io.OrderDate >= '2011-08-03 00:00:00.000'
and io.OrderDate <= '2011-08-03 23:59:59.999'

将包括任何遗漏的额外记录

关于sql - 这些日期 where 子句有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6943853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com