我用两种不同的方式编写了相同的join:
select
one.first,
two.second,
three.third
from
onetable one
inner join twotable two
on one.twokey = two.onekey
inner join threetable three
on two.twokey = three.twokey
and one.onekey = three.onekey
where 1=1
and one.firstnamefield in ('first name', 'second name', 'third name')
and two.secondnamefield not in ('last name', 'sirname', 'family name')
and three.thirdnamefield = ('nick name', 'pen name', 'handle')和
select
one.first,
two.second,
three.third
from
onetable one
inner join twotable two
on one.twokey = two.onekey
and one.firstnamefield in ('first name', 'second name', 'third name')
and two.secondnamefield not in ('last name', 'sirname', 'family name')
inner join threetable three
on two.twokey = three.twokey
and one.onekey = three.onekey
and three.thirdnamefield = ('nick name', 'pen name', 'handle')如您所见,第一个查询中的WHERE部分进入重写的查询中的JOIN。
这两者之间有什么区别吗?
一种比另一种更可取吗?
发布于 2011-04-12 01:37:53
由于您使用的是内部联接,因此结果不会有差异。然而,如果这些是Left Joins,结果可能会有很大的不同。
我倾向于将与连接相关的条件放在join子句中,将与整体过滤相关的条件放在Where子句中(这是一个细微的区别)。所以,我倾向于第一种形式。
发布于 2011-04-12 01:39:04
LEFT JOIN.发布于 2011-04-12 01:37:37
只要您正在进行等联接(而不是外联接),两者之间就没有功能上的区别。
一般来说,我更喜欢前一种方法(尽管没有无关的1=1子句)。与其他联接语法相比,SQL 92联接语法的优点之一是它允许您将联接条件与筛选条件分开。您的第一条语句实现了这种分离。您的第二条语句结合了连接和过滤条件,这使得乍一看很难确定表之间的关系以及数据是如何被过滤的。
https://stackoverflow.com/questions/5625154
复制相似问题