-1

我正在尝试编写一个 WHERE 子句,如果该列为空,它将返回所有数据。我有一个 csv 文件(static_table),其中包括:

在此处输入图像描述

如果权限为空,则意味着用户需要查看所有内容。因此,如果 hvfg23 用户登录并运行具有国家、地区和品牌的查询,则无论何时执行查询,用户都可以看到每个国家、欧洲地区和所有品牌的数据。

我现在的查询看起来像这样:

select * from t1
where(select restriction, type from t2 where user_id = {{current_userId()}} )

我无法将查询调整为我所需要的,非常感谢您的帮助。

4

1 回答 1

0

如果我很了解您的表的结构,那么这将选择当前用户的权限,然后选择其相关信息,以防它不为空,否则选择列,这意味着该列没有条件

declare @country nvarchar(50), @region nvarchar(50), @brand nvarchar(50)

set @country=(Select  [t2].[permission] 
    from [t2] where [t2].[type]='country' 
    and [t2].[user_id] = {{current_userId()}})

set @brand=(Select  [t2].[permission] 
    from [t2] where [t2].[type]='brand' 
    and [t2].[user_id] =  {{current_userId()}})

set @region=(Select  [t2].[permission] 
    from [t2] where [t2].[type]='region'
    and [t2].[user_id] =  {{current_userId()}})

select * from [t1] where
[t1].[country]= case when @country IS NULL
    then [t1].[country]  else @country end 
and [t1].[brand]= case when @brand IS NULL 
    then [t1].[brand]  else @brand end
and [t1].[region]= case when @region IS NULL
    then [t1].[region]  else @region end

于 2021-10-28T13:21:42.157 回答