【问题标题】:Show null values from a set of rows if I DON'T know the value? [closed]如果我不知道值,则显示一组行中的空值? [关闭]
【发布时间】:2021-09-19 10:17:53
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 您是否阅读过关于您之前使用nolockwilly nilly 提出的关于NOT 的问题的评论?
  • 请提供minimal reproducible example 即示例数据、带有参数的示例代码和所需结果 - 因为我根本不清楚您想要什么。
  • 这与您之前的问题有何不同?
  • 我确实回复了您在 your previous question 上的评论:“如果您不认识他们,当然不能向他们展示。我并没有真正理解您想说的话”我害怕。”。所以,你应该用一个全面的minimal reproducible example 来详细说明你的意思。 (是的,还有NOLOCK@DaleK 再次提醒你的事情......)
  • 请阅读this,了解一些改进问题的技巧。

标签: sql sql-server tsql null notin


【解决方案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'),
('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

【讨论】:

  • right join 只是一个 left join 反过来,当有更多 inner joins 时,你会感到困惑,这就是为什么没有人使用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-17
相关资源
最近更新 更多