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



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

如果我不知道上面的3个值,我如何从现有的值中显示它们(因为搜索时它们是空值(?

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

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

select *
from dataentry with (nolock)
where clientnumber in (
'00602',
'00897',
'00940',
'22234',
'87669'
)

我认为您可以使用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'),
('00897'),
('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

最新更新