作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 SQL Server 中,我有以下设计:
是否 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/
我是一名优秀的程序员,十分优秀!