Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 为什么我的大于SQL查询不起作用?_Sql Server - Fatal编程技术网

Sql server 为什么我的大于SQL查询不起作用?

Sql server 为什么我的大于SQL查询不起作用?,sql-server,Sql Server,在下面的查询中,我想将外部联接留在表3中。这不应该导致所有ID大于表1中表ID的行 这应该会返回表3中大于表1 ID的所有行。那么情况怎么会不是这样呢 问题:这是否应该返回所有ID为 是否大于表1 ID? 注意:我知道在from子句中切换表的顺序 将更改生成的数据集 select t1.ID, t1.Value, t3.ID, t3.Value from table1 as t1 left outer join table3 as t3 on t1.I

在下面的查询中,我想将外部联接留在表3中。这不应该导致所有ID大于表1中表ID的行

这应该会返回表3中大于表1 ID的所有行。那么情况怎么会不是这样呢

问题:这是否应该返回所有ID为 是否大于表1 ID?

注意:我知道在from子句中切换表的顺序 将更改生成的数据集

select t1.ID, t1.Value, 
       t3.ID, t3.Value 
from table1 as t1
       left outer join 
     table3 as t3 on t1.ID > t3.ID;
结果:

  1 First   NULL NULL
  2 Second     1 First
 ID Value
  1 First
  2 Second
 ID Value
  1 First
  2 Second
  3 Third
  4 Fourth
  5 Fifth
  6 Sixth
  7 Seventh
  8 Eighth
  1 First   2   Second
  1 First   3   Third
  1 First   4   Fourth
  1 First   5   Fifth
  1 First   6   Sixth
  1 First   7   Seventh
  1 First   8   Eighth
  2 Second  3   Third
  2 Second  4   Fourth
  2 Second  5   Fifth
  2 Second  6   Sixth
  2 Second  7   Seventh
  2 Second  8   Eighth
表1:

  1 First   NULL NULL
  2 Second     1 First
 ID Value
  1 First
  2 Second
 ID Value
  1 First
  2 Second
  3 Third
  4 Fourth
  5 Fifth
  6 Sixth
  7 Seventh
  8 Eighth
  1 First   2   Second
  1 First   3   Third
  1 First   4   Fourth
  1 First   5   Fifth
  1 First   6   Sixth
  1 First   7   Seventh
  1 First   8   Eighth
  2 Second  3   Third
  2 Second  4   Fourth
  2 Second  5   Fifth
  2 Second  6   Sixth
  2 Second  7   Seventh
  2 Second  8   Eighth
表3:

  1 First   NULL NULL
  2 Second     1 First
 ID Value
  1 First
  2 Second
 ID Value
  1 First
  2 Second
  3 Third
  4 Fourth
  5 Fifth
  6 Sixth
  7 Seventh
  8 Eighth
  1 First   2   Second
  1 First   3   Third
  1 First   4   Fourth
  1 First   5   Fifth
  1 First   6   Sixth
  1 First   7   Seventh
  1 First   8   Eighth
  2 Second  3   Third
  2 Second  4   Fourth
  2 Second  5   Fifth
  2 Second  6   Sixth
  2 Second  7   Seventh
  2 Second  8   Eighth

此查询返回我认为它应该用于此类型查询的结果表。但是第一个例子并不像我认为的那样有效。因为它将所有右选项卡与一个小于当前t1.ID的ID相匹配,当前t1.ID是它应该如何工作的。(向上看)


在第一种情况下,t1和t3中的每一行组合都会得到一个结果,其中t1 id更大,但如果t1中的某一行不会生成任何行,则会从t1中获取该行,并为t3行获取所有空值

因此t1中的“1,First”行不大于t3中的任何行,因此在输出中得到:

1,第一,空,空

对于t1中的“2,Second”行,它的id大于t3中的“1,First”行,因此在输出中得到以下结果:

2,第二,1,第一

这一切都是意料之中的


您的第二个查询也按预期工作-对于您拥有的数据集,结果与使用内部联接而不是左联接时完全相同,由于t1中没有任何行与t3中的联接谓词至少不匹配。

因此,您认为第一个结果集有什么问题?
x>y
表示
x
大于
y
。再次查看
两侧的内容。