如果我不知道值,则显示一组行中的空值?

问题描述

所以背景是 clientnumber 是客户端发送给我的值,并将插入到数据库中。但是上面的 3 个数字还没有被我插入到数据库中,所以当我在数据库搜索它们时它会变成空白。 sql 有没有办法将这些数字显示为空值?

如果我不知道上述 3 个值,我如何从确实存在的值中显示它们(因为它们在搜索时为空值)?

也许要做到这一点是使用 not IN 和我已经插入的客户号码?但是假设我插入了 2000 个数字,这将是非常低效的。这样做的最佳方法是什么?

假设以下是我知道的值,但其中两个为空,我如何只显示空值?

{{1}}

解决方法

我认为您可以使用 right join

--Let your table only contain these three number 00602,00897,00940
Create Table #temp
(
   clientnumber varchar(10)
)
INSERT INTO #temp
values
('00602'),('00897'),('00940')

--Here is your values that you want to test
Declare @table table
(
   clientnumber varchar(10)
)
INSERT INTO @table
values
('00602'),('00940'),('22234'),('87669')

--the following shows the values that does not exists on your table
select ta.clientnumber 
from #temp as tm
right join @table as ta
on tm.clientnumber =ta.clientnumber
where tm.clientnumber is null

--the following shows all values and there are two null values due to no any match on your table
select tm.clientnumber 
from #temp as tm
right join @table as ta
on tm.clientnumber =ta.clientnumber


DROP TABLE #temp