简体   繁体   English

SQL与运算符和“> =”&“<=”运算符之间的区别

[英]Difference in SQL Between operator and “>=” & “<=” operator

We are using a SQL query to search on the basis of dateFrom and dateTo fields. 我们使用SQL查询来搜索dateFrom和dateTo字段。 for that i am using " greater than equal to(>=) " and " less than equal to(<=) " operator to search on the date fields. 因为我使用“ 大于等于(> =) ”和“ 小于等于(<=) ”运算符来搜索日期字段。 somewhere i also find that we can also use SQL " Between " operator to do the same. 某处我也发现我们也可以使用SQL“ Between ”运算符来做同样的事情。 just wanted to confirm that is there any difference when we use " Between " operator and when we used " (>= & <=) " operator. 只是想确认当我们使用“ Between ”运算符和使用“ (> =&<=) ”运算符时有什么区别。

There is no difference. 没有区别。

(x BETWEEN y AND z)

is the same as writing 和写作一样

((x >= y) AND (x <= z))

不这样没有区别(当使用ORACLE时)但使用BETWEEN运算符是比较范围值的更优雅方式。

Modern databases ship with very intelligent query execution optimisers. 现代数据库附带非常智能的查询执行优化器。 One of their main features is query transformation. 它们的主要特征之一是查询转换。 Logically equivalent expressions can usually be transformed into each other. 逻辑上等效的表达式通常可以相互转换。 eg as Anthony suggested, the BETWEEN operator can be rewritten by Oracle (and MySQL) as two AND -connected comparisons, and vice versa, if BETWEEN isn't just implemented as syntactic sugar. 例如,正如Anthony建议的那样, BETWEEN运算符可以被Oracle(和MySQL)重写为两个AND连接的比较,反之亦然,如果BETWEEN不仅仅是作为语法糖实现的话。

So even if there would be a difference (in performance), you can be assured that Oracle will very likely choose the better option. 因此,即使存在差异(性能),您也可以放心,Oracle很可能会选择更好的选择。

This means that you can freely choose your preference, eg because of readability. 这意味着您可以自由选择您的偏好,例如因为可读性。

Note: it is not always obvious, what's logically equivalent. 注意:它并不总是显而易见的,逻辑上等同于什么。 Query transformation rules become more complex when it comes to transforming EXISTS , IN , NOT EXISTS , NOT IN ... But in this case, they are. 当转换EXISTSINNOT EXISTSNOT IN时,查询转换规则变得更加复杂......但在这种情况下,它们是。 For more details read the specification (chapter 8.3 between predicate): 有关更多详细信息,请阅读规范(谓词之间的第8.3章):

http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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