我正在绕圈子,希望有人能阐明当第一个查询返回空值时如何运行第二个查询。
select employee, date, workingrule
case when workingrule is null
then 1 else (select employeenumber, bonusrule from employeelist)
end
from employeedetails
基本上,如果工作规则是空白的,我想找到员工奖金规则。非常感谢任何指导帮助。谢谢
我正在绕圈子,希望有人能阐明当第一个查询返回空值时如何运行第二个查询。
select employee, date, workingrule
case when workingrule is null
then 1 else (select employeenumber, bonusrule from employeelist)
end
from employeedetails
基本上,如果工作规则是空白的,我想找到员工奖金规则。非常感谢任何指导帮助。谢谢
这适用于 SQL Server(因为使用isnull
,但可以将其更改为任何 RDBMS 特定查询):
select
ed.employee, ed.date,
isnull(ed.workingrule, el.bonusrule) as [rule]
from employeedetails as ed
left outer join employeelist as el on el.employeenumber = ed.employeenumber
或者如果你想要两者(应该适用于任何 RDBMS):
select
ed.employee, ed.date,
ed.workingrule,
case when ed.workingrule is null then '1' else el.bonusrule end as bonusrule
from employeedetails as ed
left outer join employeelist as el on el.employeenumber = ed.employeenumber
select
employee, date, Coalesce(workingrule, BonusRule) as Rule
from
employeedetails ed
left join employeelist el on
el.employeenumber = ed.employeenumber