使用where变量返回null结果



如果结果返回空,我想查看我在where子句中使用的值

select *
from (select PartNum, PONum from db where PartNum = '12345' and PONum = 'POX123' union
select PartNum, PONum from db where PartNum = '67890' and PONum = 'POX456' union
select PartNum, PONum from db where PartNum = '98765' and PONum = 'POX789') d
where PartNum is null

PartNum 98765不存在于数据库中,这正是我需要返回到其他地方进行比较的地方,这显然不起作用,但我无法思考如何返回空行,这似乎违反了直觉。

您可以枚举值,然后使用not exists(或反left join(过滤表中不存在的值。

在SQL Server中:

select p.partNum
from (values (12345), (67890), (98765)) p(partNum)
where not exists (select 1 from db d where d.partNum = p.partNum)

在Oracle中:

select p.partNum
from (
select 12345 partNum from dual
union all select 67890 from dual
union all select 98765 from dual
) p
where not exists (select 1 from db d where d.partNum = p.partNum)

请注意,这将partNum视为一个数字,因为它看起来确实像一个数字。如果它实际上是类似字符串的数据类型,那么可以用单引号将值括起来。

最新更新