我试图将 BETWEEN 与列名而不是直接值一起使用,如下所示:
SELECT * FROM table WHERE column1 BETWEEN column2 AND column3;
这将返回 17 行之类的内容,但如果我写:
SELECT * FROM table WHERE (column1 <= column2 AND column1 >= column3) OR (column1 >= column2 AND column1 <= column3)
我会得到大约 600 行..
在这两种情况下,我只得到列 1 值实际上是中间值的行,但第二种方法给了我更多的结果,所以第一种方法有问题。
我怀疑问题可能出在使用带有列名的 BETWEEN 子句,而不是纯值,并且不知何故 SQL 正在将列名转换为实际值..这很奇怪,但有人可以启发我吗?
谢谢
I'm trying to use the BETWEEN with column names instead of direct values, something like this:
SELECT * FROM table WHERE column1 BETWEEN column2 AND column3;
This is returning something like 17 rows, but if i write:
SELECT * FROM table WHERE (column1 <= column2 AND column1 >= column3) OR (column1 >= column2 AND column1 <= column3)
i get around 600 rows..
In both cases i only get rows where column1 value is actually the middle value, but 2nd method gives me much more results, so 1st method has something wrong with it.
I suspect the problem might be on using BETWEEN clause with column names, instead of pure values, and somehow SQL is converting the column names to actual values..its strange, but can someone enlighten me please?
Thanks
发布评论
评论(3)
是“或”的结果相同
与由于您的添加检查
,您将获得额外的行。
is same as
Because of your addition check of
which is
OR
ed, you get additional rows.Between A And B
假定A,即 Between 中的第一个表达式 (
A
) 小于第二个表达式表达式,(B
) 它不会检查或执行相反的选项。例如,如果您输入
Where 3 Between 4 And 2
不会返回任何行:或者,如果您输入
Select Case When 3 Between 4 and 2 then 'true ' else 'false' end
它将返回
false
Between A And B
assumes thatA<B
, i.e., that the first expression in the Between, (A
), is less than the second expression, (B
) it does not check or execute with the opposite option.e.g., if you put
Where 3 Between 4 And 2
no rows will be returned:or, if you write
Select Case When 3 Between 4 and 2 then 'true' else 'false' end
it will return
false
这两个语句的逻辑不一样:
有两个子句。删除第一个,您应该得到与您的 Between 语句相同的结果。
Your logic for the two statements is not the same:
Has two clauses. Remove the first and you should have the same results as your between statement.