当所选字段为空时,从另一个字段中选择值

时间:2014-03-06 21:06:26

标签: ms-access

在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;

2 个答案:

答案 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功能。它就像IsNullcoalesce

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;