Oracle查询中的条件WHERE EXISTS

热肉丸汤

甲骨文在这里。我有下表:

create table Foobars ( 
    id integer constraint pkFoobars primary key using index,
    fizz varchar2(20 char), 
    buzz varchar2(20 char)
)

在我的代码中,将给我一个名为的字符串fizzOrBuzz,并且我不知道它将在哪个字段(fizzbuzz)之前。我需要搜索Foobars表,要么匹配fizzbuzz基于的fizzOrBuzz价值。唯一可以确定的是没有重复项(没有2个记录的fizz相同,也没有2个记录的buzz相同)。

到目前为止,我的查询:

SELECT
  id
FROM
  Foobars
WHERE EXISTS (
  SELECT
    id
  FROM
    Foobars
  WHERE
    fizz = ? -- fizzOrBuzz gets injected here by the app layer
) OR (
  SELECT
    id
  FROM
    Foobars
  WHERE
    buzz = ? -- fizzOrBuzz gets injected here by the app layer  
)

但是,这不是有效的SQL代码(无法执行),我敢肯定,有更好的方法可以完全做到这一点。有任何想法吗?

克里斯·萨克森

我不确定您为什么需要它exists,当然or这里有一些基本的方法

select id
from   foobars
where  fizz = ? -- fizzOrBuzz gets injected here by the app layer
or     buzz = ? -- fizzOrBuzz gets injected here by the app layer  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章