gpt4 book ai didi

SQL : First condition in OR statement in JOIN is always executed first?

转载 作者:行者123 更新时间:2023-12-02 16:05:24 24 4
gpt4 key购买 nike

在 SQL Server 中,我有以下设计:

enter image description here

是否 100% 确定 JOIN 中 OR 语句的第一个条件将首先执行?那么下面的SQL语句会得到绿色的结果吗?

SELECT P.Name, D.Percentage
FROM Personnel P
JOIN Department D ON
P.Dep_Code = D.Code AND
(P.SubDep_Code = D.SubCode OR D.SubCode = '*')

最佳答案

Is it 100% sure that the first condition from the OR statement in aJOIN will be executed first ?

没有。没有保证的计算顺序,即使整个表达式仍然计算为相同的值,并且不会影响连接中匹配的行。

您的查询将给出以下结果:

Name       Percentage
---------- -----------
P-A 100
P-A 20
P-A 80
P-B 100

我猜您正在寻找类似的东西。

select P.Name,
coalesce(D1.Percentage, D2.Percentage) as Percentage
from Personnel as P
left outer join Department as D1
on P.Dep_Code = D1.Code and
P.SubDep_Code = D1.SubCode
left outer join Department as D2
on P.Dep_Code = D2.Code and
D2.SubCode = '*'
where coalesce(D1.Percentage, D2.Percentage) is not null

您可以在此处使用 SQL Server 2008 尝试查询。https://data.stackexchange.com/stackoverflow/qt/118492/

关于SQL : First condition in OR statement in JOIN is always executed first?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8208731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com