5
SELECT *
FROM Tabl tabb
WHERE (tabb.col1, tabb.col2) IN ( (1,2), (3,4))

The above works in Oracle but I am trying to run in a proprietary SQL engine which doesn't support the above query formation with multiple columns in IN.

I am trying to find combinations of 1,2 and 3,4 in the DB.

Please help me with any alternatives for achieving the above.

I am looking for ways to pass a list of values for col1 and col2 from Java at one shot, so '=' might not be an option as it might require two SQL statements for achieving the above.

3
  • it would be easier with sample data, you want the result where col1 = 1 and col2 = 2 or col1 = 3 and col2 = 4 ? Commented Nov 27, 2012 at 21:12
  • Yes I want the result where col1 = 1 and col2 = 2 or col1 = 3 and col2 = 4. Commented Nov 27, 2012 at 21:19
  • I think @hall stephenk answer is correct... Commented Nov 27, 2012 at 21:29

3 Answers 3

5

How about this?

SELECT
    *
FROM
    Tabl tabb
WHERE
    (tabb.col1 = 1 AND tabb.col2 = 2) OR
    (tabb.col1 = 3 AND tabb.col2 = 4)
Sign up to request clarification or add additional context in comments.

1 Comment

Apologies for not being clearer earlier, please see the edited question above.
0

You could do something like this:

SELECT *
FROM Tabl tabb
inner join (
    select 1 a, 2 b 
    union select 3, 4
) x
on tabb.col1 = x.a
and tabb.col2 = x.b

1 Comment

Like I mentioned in the question, the values 1,2,3,4 are static values and not a result of select.
0

In case of numeric columns and supposing the values are bounded to a specific range more values can be packed into one number using some formula

for instance supposing the values of col1 and col2 are from 1 to 1000 the formula

  N = 10000 * col1 + col2

can be used and in the example the select would be

  SELECT * FROM Tabl tabb WHERE (10000 * tabb.col1 + tabb.col2) IN ( 10002, 30004)

In general string concatenation can be used to combine several column values, for example

  SELECT * FROM Tabl tabb WHERE (tabb.col1||"&"||tabb.col2) IN ( "1&2", "3&4")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.