如何正确处理查询约束中的日期
我目前正在使用以下查询来完成 2010 年 6 月的所有检查:
select inspections.name
from inspections
where
to_char(inspections.insp_date, 'YYYY') = 2010 and
to_char(inspections.insp_date, 'MM') = 06;
但这感觉有点尴尬。难道没有更好的方法吗?查看 http://infolab.stanford.edu/~ullman/fcdb /oracle/or-time.html 似乎并非如此。我正在使用 Oracle,如果它有什么区别的话。
谢谢
I am currently using the following query to get all the inspections done on june 2010:
select inspections.name
from inspections
where
to_char(inspections.insp_date, 'YYYY') = 2010 and
to_char(inspections.insp_date, 'MM') = 06;
but this feels kinda awkward. Wouldn't there be a better way of doing this? Looking at http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html it doesn't seem so. I am using Oracle, if it makes a difference.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我喜欢尽可能使用范围比较,因为优化器可以将其用于索引扫描:
I like to use range comparison when possible since this can be used for index-scan by the optimizer:
我同意文森特的答案,但为了完整起见,您可以将您的答案简化为:
甚至可以使用索引,前提是索引是:
I agree with Vincent's answer, but for completeness you could simplify yours to:
That could even use an index, provided the index were:
另一种选择是
从效率的角度来看,
Another option would be
From an efficiency perspective