我正在 SQL Server 中创建一个简单的过程,如下所示。
DECLARE @num int;
SET @num = 5;
SELECT @num WHERE @num BETWEEN 1 AND 10;
SELECT @num WHERE @num BETWEEN 10 AND 1;
如果运行此命令,第一个 select 语句会返回 5,而第二个 select 语句不会返回任何内容。
我很困惑为什么会这样,因为这两种情况都应该返回 true,因为 5 介于 10 和 1 以及 1 和 10 之间。
是否有一个原因BETWEEN 10 AND 1
线条违背逻辑?
Thanks
这就是 SQL 的标准(已决定的标准)。
x BETWEEN a AND b
代表
( a <= x ) AND ( x <= b )
参见第 211 页:SQL-92 规范(审查草案副本)
8.3 <between predicate>
Function
Specify a range comparison.
Format
<between predicate> ::=
<row value constructor> [ NOT ] BETWEEN
<row value constructor> AND <row value constructor>
Syntax Rules
1) The three <row value constructor>s shall be of the same degree.
2) Let respective values be values with the same ordinal position
in the two <row value constructor>s.
3) The data types of the respective values of the three <row value
constructor>s shall be comparable.
4) Let X, Y, and Z be the first, second, and third <row value con-
structor>s, respectively.
5) "X NOT BETWEEN Y AND Z" is equivalent to "NOT ( X BETWEEN Y AND
Z )".
6) "X BETWEEN Y AND Z" is equivalent to "X>=Y AND X<=Z".
我所知道的唯一不符合标准(并遵循您的逻辑)的 RDBMS 是 MS-Access。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)