在ms-access中,我想在所选字段为空时从另一个字段中选择值。 因此,当所有者为空时,我希望选择管理员名称。
SELECT
[Master Bench Assignments].Active,
[Client/Projects].Client,
Employees.[Last Name],
Employees.[First Name],
[Client/Projects].Project,
[Bench Assignments].Name,
[Bench Assignments].[Long Description],
[Master Bench Assignments].Notes,
[Bench Assignments].Owner,
[Staff Managers].[Last Name]
FROM [Client/Projects]
RIGHT JOIN ([Staff Managers]
RIGHT JOIN (Employees RIGHT JOIN ([Bench Assignments]
LEFT JOIN [Master Bench Assignments] ON [Bench Assignments].[Bench ID] = [Master Bench Assignments].[Bench Task ID]) ON Employees.[Employee ID] = [Master Bench Assignments].[Employee ID])
ON [Staff Managers].[Staff Manager ID] = Employees.[Staff Manager ID])
ON [Client/Projects].[Project ID] = [Master Bench Assignments].[Client/Project]
WHERE ((([Master Bench Assignments].Active)=Yes)
AND (([Master Bench Assignments].Status)="Bench"))
ORDER BY [Bench Assignments].Name;
答案 0 :(得分:2)
这是一个简单的例子,它展示了我认为你想做的事情。
SELECT
p.owner,
p.manager,
Nz(p.owner, p.manager) AS the_decider
FROM Projects AS p;
这是该查询的输出,带有2行样本表:
owner manager the_decider
----- ------- -----------
a b a
c c
如果您要从Access会话外部运行查询,则可以使用IIf
代替Nz
。
IIf(p.owner Is Null, p.manager, p.owner) AS the_decider
答案 1 :(得分:1)
查看Nz
功能。它就像IsNull
或coalesce
SELECT
[Master Bench Assignments].Active,
[Client/Projects].Client,
Employees.[Last Name],
Employees.[First Name],
[Client/Projects].Project,
[Bench Assignments].Name,
[Bench Assignments].[Long Description],
[Master Bench Assignments].Notes,
Nz([Bench Assignments].Owner, [Staff Managers].[Last Name]) as [columnName]
[Staff Managers].[Last Name]
FROM [Client/Projects]
RIGHT JOIN ([Staff Managers]
RIGHT JOIN (Employees RIGHT JOIN ([Bench Assignments]
LEFT JOIN [Master Bench Assignments] ON [Bench Assignments].[Bench ID] = [Master Bench Assignments].[Bench Task ID]) ON Employees.[Employee ID] = [Master Bench Assignments].[Employee ID])
ON [Staff Managers].[Staff Manager ID] = Employees.[Staff Manager ID])
ON [Client/Projects].[Project ID] = [Master Bench Assignments].[Client/Project]
WHERE ((([Master Bench Assignments].Active)=Yes)
AND (([Master Bench Assignments].Status)="Bench"))
ORDER BY [Bench Assignments].Name;