T-SQL BETWEEN problem max value first(T-SQL BETWEEN 问题最大值优先)
问题描述
为什么这两个表达式返回不同的结果?这真的很愚蠢.
Why this two expressions return different results? This is really stupid.
SELECT * FROM Table WHERE ID BETWEEN 3 AND 1
SELECT * FROM Table WHERE ID BETWEEN 1 AND 3
推荐答案
作为 文档说:
如果 test_expression 的值大于,BETWEEN 返回 TRUE或等于 begin_expression 的值且小于或等于end_expression 的值.
BETWEEN returns TRUE if the value of test_expression is greater than or equal to the value of begin_expression and less than or equal to the value of end_expression.
没有说明交换 start_expression 和 end_expression 以匹配最小值和最大值.您应该期待记录在案的结果,而不是您认为应该得到的结果.
Doesn't say anything about swapping the start_expression and end_expression to match min and max values. You should expect the result as documented, not as you believe it should.
对于那些好奇的人,ANSI SQL99 标准指定 BETWEEN 谓词应该包括一个用于 SYMMETRIC 或 ASYMMETRIC 比较的子句.只有 SYMMETRIC 允许交换 start_range 和 end_range,ASYMMETRIC 需要严格.ASYMMETRIC 形式是隐式形式.换句话说,将 A BETWEEN X and Y
解释为 (A>=X AND A<=Y) OR (A>=Y AND A<=X)
,正如 OP 所暗示的那样,不符合标准.
For the curious out there, the ANSI SQL99 standard specifies that the BETWEEN predicate should include a clause for SYMMETRIC or ASYMMETRIC comparison. Only the SYMMETRIC one is allowed to swap the start_range and end_range, the ASYMMETRIC one is required to be strict. The ASYMMETRIC form is the implicit form. In other words an implementation that interprets A BETWEEN X and Y
as (A>=X AND A<=Y) OR (A>=Y AND A<=X)
, as the OP suggests, is not standard compliant.
这篇关于T-SQL BETWEEN 问题最大值优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:T-SQL BETWEEN 问题最大值优先


- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01