SQL之间的from和to值的顺序

Siv*_*esh 3 sql sql-server

我正在SQL Server中创建一个简单的过程,如下所示.

DECLARE @num int;
SET @num = 5;
SELECT @num WHERE @num BETWEEN 1 AND 10;
SELECT @num WHERE @num BETWEEN 10 AND 1;
Run Code Online (Sandbox Code Playgroud)

如果你运行它,第一个select语句给你5,第二个没有返回任何内容.我很困惑为什么会这样,因为两个案例都应该返回true,因为5介于10和1以及1和10之间.

BETWEEN 10 AND 1条线是否有理由违背逻辑?

谢谢

ype*_*eᵀᴹ 6

这就是SQL的标准(决定).

x BETWEEN a AND b
Run Code Online (Sandbox Code Playgroud)

代表

( a <= x )  AND  ( x <= b )
Run Code Online (Sandbox Code Playgroud)

请参见第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".
Run Code Online (Sandbox Code Playgroud)

我所知道的唯一一个远离标准(并遵循你的逻辑)的RDBMS是MS-Access.