无法执行隐式SQL连接的查询? [英] Queries that implicit SQL joins can't do?

查看:110
本文介绍了无法执行隐式SQL连接的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有学过联接的工作方式,而只是使用select和where子句就足以完成我所做的所有查询.在某些情况下,我无法使用WHERE子句获得正确的结果,而不得不使用JOIN吗?如果是这样,有人可以提供示例吗?谢谢.

I've never learned how joins work but just using select and the where clause has been sufficient for all the queries I've done. Are there cases where I can't get the right results using the WHERE clause and I have to use a JOIN? If so, could someone please provide examples? Thanks.

推荐答案

隐式连接已过期20多年了.您为什么还要考虑与他们一起编写代码?

Implicit joins are more than 20 years out-of-date. Why would you even consider writing code with them?

是的,他们可以创建显式联接所没有的问题.说到SQL Server,不能保证左右联接隐式语法可以返回正确的结果.有时,它们返回交叉联接而不是外部联接.这是一件坏事.至少从SQL Server 2000开始就是如此,并且它们已经被逐步淘汰,因此使用它们是一种很糟糕的做法.

Yes, they can create problems that explicit joins don't have. Speaking about SQL Server, the left and right join implicit syntaxes are not guaranteed to return the correct results. Sometimes, they return a cross join instead of an outer join. This is a bad thing. This was true even back to SQL Server 2000 at least, and they are being phased out, so using them is an all around poor practice.

隐式联接的另一个问题是,很容易通过忘记where条件之一而意外地进行交叉联接,尤其是在联接太多表时.通过使用显式联接,如果您忘记放入联接条件,并且必须这样显式指定交叉联接,则会出现语法错误.同样,这会导致查询返回错误的值,或者通过使用distinct来消除效率最高的交叉联接来进行修复.

The other problem with implicit joins is that it is easy to accidentally do a cross join by forgetting one of the where conditions, especially when you are joining too many tables. By using explicit joins, you will get a syntax error if you forget to put in a join condition and a cross join must be explicitly specified as such. Again, this results in queries that return incorrect values or are fixed by using distinct to get rid of the cross join which is inefficient at best.

此外,如果您有交叉联接,那么当您使用隐式联接时,一年来进行更改的维护开发人员将不知道是否打算这样做.

Moreover, if you have a cross join, the maintenance developer who comes along in a year to make a change doesn't know if it was intended or not when you use implicit joins.

我相信某些ORM现在也需要显式联接.

I believe some ORMs also now require explicit joins.

此外,如果由于不了解联接的运行方式而使用隐式联接,则很有可能编写的代码实际上无法返回正确的结果,因为您不知道如何评估什么正确的结果是,因为您不了解联接的含义.

Further, if you are using implied joins because you don't understand how joins operate, chances are high that you are writing code that, in fact, does not return the correct result because you don't know how to evaluate what the correct result would be since you don't understand what a join is meant to do.

如果您编写任何形式的SQL代码,都没有充分理解联接的借口.

If you write SQL code of any flavor, there is no excuse for not thoroughly understanding joins.

这篇关于无法执行隐式SQL连接的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆