SQL - 连接之间与位置之间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29011643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:24:25  来源:igfitidea点击:

SQL - JOIN BETWEEN vs WHERE BETWEEN

sqlsql-serverjoinwherebetween

提问by RoyalSwish

A few weeks ago I did a query (can't remember what for) and it involved the BETWEENoperator. At first, I used this in the WHEREclause, but it didn't produce the correct result set (again, I don't remember what the problem was). I thought about why and eventually concluded that I had to move it to the JOINclause.

几周前,我做了一个查询(不记得是为了什么),它涉及到BETWEEN操作员。起初,我在WHERE子句中使用了这个,但它没有产生正确的结果集(再次,我不记得问题是什么)。我想了想为什么,最终得出结论,我必须把它移到JOIN子句中。

e.g.

例如

"Original" query:

“原始”查询:

SELECT --something
FROM table1 a
/*type of*/ JOIN table2 b ON a.field = b.field
WHERE b.field2 BETWEEN a.field2 AND a.field3 /*some other conditions*/

Revised query:

修改后的查询:

SELECT --something
FROM table1 a
/*type of*/ JOIN table2 b ON a.field = b.field 
                       AND b.field2 BETWEEN a.field2 AND a.field3
WHERE /*some other conditions*/

The JOINversion returned a smaller result set compared to the WHEREversion, and the JOINversion produced the correct result set whereas WHEREone didn't.

JOIN版本相比,版本返回的结果集更小WHEREJOIN版本生成了正确的结果集,而WHERE一个则没有。

The reason I'm asking now is because at the time I thought nothing of it, but today my boss questioned why I did it (for an entirely different query) and I told him the experience I had the last time I did, and both he and I are quite intrigued as to the differences between the two usages.

我现在问的原因是因为当时我什么也没想到,但是今天我的老板质疑我为什么这样做(对于一个完全不同的问题),我告诉他我上次这样做的经历,并且两者都他和我对这两种用法之间的差异非常感兴趣。

From what I think as to why one produced something different to the other is that the WHEREclause version only selecteda range of data, whereas the JOINversion looked for a range of data to joinon. Is this the case? Or I did come across a unique experience affected by the rest of the query (the query was much more complicated than the examples).

我认为为什么一个人产生与另一个不同的东西是WHERE条款版本只选择了一系列数据,而JOIN版本则寻找一系列数据来加入。是这种情况吗?或者我确实遇到了受其余查询影响的独特体验(查询比示例复杂得多)。

EDITI disagree with the possible duplicate because my one looks specifically at the BETWEENclause, not joins in general. I have read the mentioned post before and it didn't answer my question, hence why I posted this.

编辑我不同意可能的重复,因为我的一个专门查看BETWEEN子句,而不是一般的连接。我之前已经阅读了提到的帖子,但它没有回答我的问题,因此我发布了这个帖子。

回答by Matt

There's no difference between two versions, except if you have wrong precedence in one of them due to lack of parentheses.

两个版本之间没有区别,除非您在其中一个版本中由于缺少括号而具有错误的优先级。