Oracle here. I have the following table:
create table Foobars (
id integer constraint pkFoobars primary key using index,
fizz varchar2(20 char),
buzz varchar2(20 char)
)
In my code I will be given a string called fizzOrBuzz, and I will not know which field (fizz or buzz) it will be ahead of time. I need to search the Foobars table for either a matching fizz or buzz based on the fizzOrBuzz value. The only thing that is certain is that there are no duplicates (no 2 records will have the same fizz value, and no 2 records will have the same buzz value).
My query thus far:
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
)
However this isn't valid SQL code (doesn't execute) and I'm sure there's a better way of doing this altogether. Any ideas?
OR=>OR EXISTS? But the other options offered as answers are be better; not least because your subqueries aren't correlated - assuming you're expecting the ID from those to match the main query. (It's helpful to show the error you got, and sample data/expected results.)