SQL 服务器 - 添加条件内连接
SQL Server - Add conditional inner join
我是数据库开发的新手,我有一个存储过程,我需要在其中仅在满足条件时对表执行一些内部联接。以下是我的复杂查询的较短版本。
IF (testFunction(@UserId)) = 0
select C.AwardId, C.ProgramName, Count(ClientId) as Intakes from Client C
where C.Id = @ClientId
group by C.AwardId, C.ProgramName...
ELSE
select C.AwardId, C.ProgramName, Count(ClientId) as Intakes from Client C
INNER JOIN UserRoleEntity URE ON C.AwardId = URE.EntityId
INNER JOIN UserRole UR ON URE.UserRoleId = UR.Id AND UR.CDPUserId = @UserId
where C.Id = @ClientId
group by C.AwardId, C.ProgramName...
END
基本上,它在 "IF" 和 "ELSE" 中都是相同的 SELECT 查询,只是在不满足 IF 条件时我需要对 2 个表进行内部联接。不是在 "IF" 和 "ELSE" 中重复查询,有没有办法让它成为一个单一的查询而不去动态查询?
谢谢!
如果我的查询正确,您想要显示所有字段,即使 CDPUserID
不存在或 0
对吗?您只需要 FULL JOIN
在第二个连接中:
SELECT C.AwardId, C.ProgramName, Count(ClientId) as Intakes FROM Client C
INNER JOIN UserRoleEntity URE ON C.AwardId = URE.EntityId
FULL JOIN UserRole UR ON URE.UserRoleId = UR.Id AND UR.CDPUserId = @UserId
WHERE C.Id = @ClientId
GROUP BY C.AwardId, C.ProgramName...
在 where 条件中使用 OR 和 EXISTS()
select C.AwardId, C.ProgramName, Count(ClientId) as Intakes from Client C
where
(testFunction(@UserId)) = 0
OR
(
EXISTS
(
SELECT * FROM UserRoleEntity URE INNER JOIN UserRole UR ON URE.UserRoleId = UR.Id AND UR.CDPUserId = @UserId
WHERE C.AwardId = URE.EntityId
)
AND C.Id = @ClientId
)
group by C.AwardId, C.ProgramName...
您可以使用额外的 WHERE
条件和 LEFT JOIN
进行条件过滤:
SELECT C.AwardId, C.ProgramName, Count(ClientId) as Intakes
FROM Client C
LEFT JOIN UserRoleEntity URE ON C.AwardId = URE.EntityId
LEFT JOIN UserRole UR ON URE.UserRoleId = UR.Id AND UR.CDPUserId = @UserId
WHERE C.Id = @ClientId
AND ( testFunction(@UserId) = 0
OR (testFunction(@UserId) <> 0 AND URE.AwardID IS NOT NULL AND UR.ID IS NOT NULL))
GROUP BY C.AwardId, C.ProgramName
我是数据库开发的新手,我有一个存储过程,我需要在其中仅在满足条件时对表执行一些内部联接。以下是我的复杂查询的较短版本。
IF (testFunction(@UserId)) = 0
select C.AwardId, C.ProgramName, Count(ClientId) as Intakes from Client C
where C.Id = @ClientId
group by C.AwardId, C.ProgramName...
ELSE
select C.AwardId, C.ProgramName, Count(ClientId) as Intakes from Client C
INNER JOIN UserRoleEntity URE ON C.AwardId = URE.EntityId
INNER JOIN UserRole UR ON URE.UserRoleId = UR.Id AND UR.CDPUserId = @UserId
where C.Id = @ClientId
group by C.AwardId, C.ProgramName...
END
基本上,它在 "IF" 和 "ELSE" 中都是相同的 SELECT 查询,只是在不满足 IF 条件时我需要对 2 个表进行内部联接。不是在 "IF" 和 "ELSE" 中重复查询,有没有办法让它成为一个单一的查询而不去动态查询?
谢谢!
如果我的查询正确,您想要显示所有字段,即使 CDPUserID
不存在或 0
对吗?您只需要 FULL JOIN
在第二个连接中:
SELECT C.AwardId, C.ProgramName, Count(ClientId) as Intakes FROM Client C
INNER JOIN UserRoleEntity URE ON C.AwardId = URE.EntityId
FULL JOIN UserRole UR ON URE.UserRoleId = UR.Id AND UR.CDPUserId = @UserId
WHERE C.Id = @ClientId
GROUP BY C.AwardId, C.ProgramName...
在 where 条件中使用 OR 和 EXISTS()
select C.AwardId, C.ProgramName, Count(ClientId) as Intakes from Client C
where
(testFunction(@UserId)) = 0
OR
(
EXISTS
(
SELECT * FROM UserRoleEntity URE INNER JOIN UserRole UR ON URE.UserRoleId = UR.Id AND UR.CDPUserId = @UserId
WHERE C.AwardId = URE.EntityId
)
AND C.Id = @ClientId
)
group by C.AwardId, C.ProgramName...
您可以使用额外的 WHERE
条件和 LEFT JOIN
进行条件过滤:
SELECT C.AwardId, C.ProgramName, Count(ClientId) as Intakes
FROM Client C
LEFT JOIN UserRoleEntity URE ON C.AwardId = URE.EntityId
LEFT JOIN UserRole UR ON URE.UserRoleId = UR.Id AND UR.CDPUserId = @UserId
WHERE C.Id = @ClientId
AND ( testFunction(@UserId) = 0
OR (testFunction(@UserId) <> 0 AND URE.AwardID IS NOT NULL AND UR.ID IS NOT NULL))
GROUP BY C.AwardId, C.ProgramName