1

我正在编写一个看起来像这样的存储过程:

SELECT
    Positions.PositionId 
    ,Positions.Title
    ,Positions.Location
    ,Positions.Description
    ,Positions.MaximumSalary
    ,PositionsDepartments.Description Department
    ,PositionsSubDepartments.Description Subdepartment
    ,PositionsDepartments.DepartmentId DepartmentId
    ,PositionsSubDepartments.SubDepartmentId SubdepartmentId
    ,@TheRole TheRole
    ,@Essentials Essentials
    ,@Desirable Desirable
    ,Positions.Published
    ,Positions.LastUpdatedDate
    ,PositionsStatus.Description Status
FROM 
    Positions
    WITH (NOLOCK)
    INNER JOIN PositionsStatus ON Positions.StatusId = PositionsStatus.StatusId
    INNER JOIN PositionsSubDepartments ON Positions.SubDepartmentId = PositionsSubDepartments.SubDepartmentId
    INNER JOIN PositionsDepartments ON PositionsSubDepartments.DepartmentId = PositionsDepartments.DepartmentId
WHERE 
    Positions.PositionId = @PositionId

但是 Positions.SubDepartmentId 现在可能为空 - 这意味着我没有得到我期望的所有数据。我已经尝试过了,但得到了大量重复数据:

SELECT 
    Positions.PositionId
    ,Positions.Title
    ,Positions.Location
    ,Positions.Description
    ,Positions.MaximumSalary
    ,PositionsDepartments.Description Department
    ,PositionsSubDepartments.Description Subdepartment
    ,PositionsDepartments.DepartmentId DepartmentId
    ,PositionsSubDepartments.SubDepartmentId SubdepartmentId
    ,@TheRole TheRole
    ,@Essentials Essentials
    ,@Desirable Desirable
    ,Positions.Published, Positions.LastUpdatedDate
    ,PositionsStatus.Description Status
FROM 
    Positions
    WITH (NOLOCK)
    INNER JOIN PositionsStatus ON Positions.StatusId = PositionsStatus.StatusId
    INNER JOIN PositionsSubDepartments ON Positions.SubDepartmentId = PositionsSubDepartments.SubDepartmentId OR ( Positions.SubDepartmentId IS NULL)
    INNER JOIN PositionsDepartments ON PositionsSubDepartments.DepartmentId = PositionsDepartments.DepartmentId 
WHERE 
    Positions.PositionId = @PositionId

我究竟做错了什么?

4

3 回答 3

3

当条目可能为空时,您需要使用左连接

SELECT
    Positions.[PositionId]
    ,Positions.[Title]
    ,Positions.[Location]
    ,Positions.[Description]
    ,Positions.[MaximumSalary]
    ,PositionsDepartments.[Description] Department
    ,PositionsSubDepartments.[Description] Subdepartment
    ,PositionsDepartments.[DepartmentId] DepartmentId
    ,PositionsSubDepartments.[SubDepartmentId] SubdepartmentId
    ,@TheRole TheRole
    ,@Essentials Essentials
    ,@Desirable Desirable
    ,Positions.[Published]
    ,Positions.[LastUpdatedDate]
    ,PositionsStatus.[Description] Status
FROM 
    Positions WITH (NOLOCK)
    INNER JOIN PositionsStatus ON Positions.StatusId = PositionsStatus.StatusId
    LEFT JOIN PositionsSubDepartments ON Positions.SubDepartmentId = PositionsSubDepartments.SubDepartmentId
    LEFT JOIN PositionsDepartments ON PositionsSubDepartments.DepartmentId = PositionsDepartments.DepartmentId 
WHERE 
    Positions.PositionId = @PositionId

您可能还会发现以下有用:http ://www.tutorialspoint.com/sql/sql-left-joins.htm

于 2013-11-07T12:11:22.357 回答
2
select Positions.PositionId, Positions.Title, Positions.Location,Positions.Description,Positions.MaximumSalary,
PositionsDepartments.Description Department, PositionsSubDepartments.Description Subdepartment, PositionsDepartments.DepartmentId DepartmentId, PositionsSubDepartments.SubDepartmentId SubdepartmentId,
@TheRole TheRole, @Essentials Essentials, @Desirable Desirable,
Positions.Published, Positions.LastUpdatedDate, PositionsStatus.Description Status
FROM Positions
WITH (NOLOCK)
INNER JOIN PositionsStatus ON Positions.StatusId = PositionsStatus.StatusId
LEFT JOIN PositionsSubDepartments ON Positions.SubDepartmentId = PositionsSubDepartments.SubDepartmentId
LEFT JOIN PositionsDepartments ON PositionsSubDepartments.DepartmentId = PositionsDepartments.DepartmentId 
WHERE Positions.PositionId = @PositionId
于 2013-11-07T12:05:43.943 回答
0

已修复 - 感谢您的帮助:

select Positions.PositionId, Positions.Title, Positions.Location, Positions.Description, Positions.MaximumSalary, Positions.DepartmentId, Positions.SubDepartmentId,
    PositionsDepartments.Description Department, PositionsSubDepartments.Description Subdepartment, PositionsSubDepartments.SubDepartmentId SubdepartmentId,
    @TheRole TheRole, @Essentials Essentials, @Desirable Desirable,
    Positions.Published, Positions.LastUpdatedDate, PositionsStatus.Description Status
    FROM Positions
    WITH (NOLOCK)
    INNER JOIN PositionsStatus ON Positions.StatusId = PositionsStatus.StatusId
    LEFT JOIN PositionsSubDepartments 
        ON PositionsSubDepartments.SubDepartmentId = Positions.SubDepartmentId OR (Positions.SubDepartmentId IS NULL AND PositionsSubDepartments.SubDepartmentId IS NULL)
    LEFT JOIN PositionsDepartments
        ON Positions.DepartmentId = PositionsDepartments.DepartmentId
    WHERE Positions.PositionId = @PositionId
于 2013-11-07T12:53:55.613 回答