【问题标题】:Return null results using where variables使用 where 变量返回空结果
【发布时间】:2020-04-14 18:22:05
【问题描述】:

如果结果返回 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 不在数据库中,这就是我需要返回以在其他地方进行比较的内容,这显然不起作用,但我无法解决如何返回空行,这似乎违反直觉。

【问题讨论】:

  • Oracle 还是 SQL Server?请只保留相关的数据库标签。
  • 公平评论...Oracle 为胜利。

标签: sql oracle select


【解决方案1】:

您可以枚举值,然后使用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)

在甲骨文中:

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 视为一个数字,因为它看起来确实像一个数字。如果它实际上是类似字符串的数据类型,那么您可以用单引号将值括起来。

【讨论】:

  • 无论哪种方式都对我有用。我能理解这一点,这让我前进。只是需要那个轻推。谢谢。
  • 我的具体问题和问题解决了几个星期。再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2018-03-17
相关资源
最近更新 更多