2

I Have two tables,

Table1
id - BIGINT
otherKey - VARCHAR(45) <---Unique

Table2
table1ID - BIGINT

The table1ID field in table2 is the same value as the id field in Table1.

I have an array containing values corresponding to the field otherKey in Table2.

myArray = {'key1', 'key2', 'key3'}

I am trying to return all Table2 rows whose otherKey value exists in myArray. I have:

SELECT * FROM Table2 WHERE (SELECT id FROM Table1 WHERE otherKey = ANY(myArray))

But I know using '=' , means the subquery can't return multiple results.

Any suggestions would be great!

2
  • Are you just looking for WHERE table1id IN (...subquery...)? Commented Dec 16, 2013 at 15:37
  • Wow yeah, I guess I am, if you want to make it an answer I'll accept it. Commented Dec 16, 2013 at 15:42

1 Answer 1

1

You are quite right, if you want the outer subquery to match multiple rows from the inner subquery then equals isn't up to the job.

Which is why they invented IN.

SELECT * FROM Table2 WHERE table1id IN (SELECT ...)
Sign up to request clarification or add additional context in comments.

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.