【发布时间】:2021-02-05 00:51:47
【问题描述】:
我想知道如何在下面的查询中使用 if/else 或 case 语句或任何可以达到目的的语句。
这个想法是,每当关键字为 NULL 或空白时,我都不想在 Where 语句中包含 pv.AdminPost = 1 and pv.IsStaff = 1 条件。
有什么办法吗?谢谢。
;WITH cte
AS (SELECT fv.Id, fv.[Description]
FROM FeedView fv
WHERE (fv.Id = @PostId OR fv.[Description] LIKE '%' + @Keyword + '%' OR @Keyword IS NULL ) AND fv.ForUserId = @UserId
UNION
SELECT pv.Id, pv.[Description]
FROM PublicView pv
WHERE @InculdePublicPosts = 1 -- This acts as an off switch to decide whether to include public post or not.
OR pv.AdminPost = 1 OR pv.IsStaff = 1 -- how to remove this condition when Keyword is NULL or empty
AND (pv.Id = @PostId OR pv.[Description] LIKE '%' + @Keyword + '%' OR @keyword IS NULL )
)
SELECT cte.Id, cte.[Description]
FROM .....
【问题讨论】:
-
请提供样本数据、期望的结果以及您想要的逻辑的清晰解释。
标签: sql sql-server where-clause