gpt4 book ai didi

mysql - 如何在 SQL where 子句中使用 if 条件应用用户过滤器?

转载 作者:行者123 更新时间:2023-11-29 18:22:15 26 4
gpt4 key购买 nike

我发现了一堆类似的questions我有,但无法得到确切的答案!

我在 SQL where 子句中有以下条件

declare @status int, @applyUserFilter bit, @user nvarchar(156)

--following are values being set by UI
set @activeStatus = 1
set @applyUserFilter = 1
set @user = 'xyzUser'

--I want to filter the list by users only when @applyUserFilter flag is true otherwise it should return the list for all users

--This is what i tried but gives syntax error
WHERE ActiveStatus = @activeStatus
AND CASE WHEN @applyUserFilter = 1 THEN User = @user

注意:这仅显示 where 子句代码!

最佳答案

关于:

    WHERE activeStatus=@activeStatus
AND (user IN (SELECT user from #users) AND @applyUserFilter=0
OR user=@user)

关于mysql - 如何在 SQL where 子句中使用 if 条件应用用户过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46474585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com