简体   繁体   English

SQL - 如果列为空,则列出所有数据

[英]SQL - list all data if a column is empty

I'm trying to write a WHERE clause, which returns all data if the column is empty.我正在尝试编写一个 WHERE 子句,如果该列为空,它将返回所有数据。 I have a csv file (static_table) which consists of:我有一个 csv 文件(static_table),其中包括:

在此处输入图片说明

If the permission is empty that means that the user will need to see everything.如果权限为空,则意味着用户需要查看所有内容。 So if the hvfg23 user is logged in and runs a query that has country, region and brand, whenever the query is executed the user can see data for every country, for Europe region and all brands.因此,如果 hvfg23 用户登录并运行包含国家、地区和品牌的查询,则无论何时执行查询,用户都可以看到每个国家、欧洲地区和所有品牌的数据。

The query I have at the moment looks something like this:我目前的查询如下所示:

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

I'm having trouble adjusting the query to what exactly I need, the help will be very appreciated.我在将查询调整为我需要的内容时遇到问题,非常感谢您的帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM