5

I have a query like this:

SELECT c1 from t WHERE c2 IN list1 AND c3 IN list1;

I want to combine this query to get something like this:

SELECT c1 from t WHERE c2 AND c3 IN list1;
3
  • 2
    What is the real problem? Commented Mar 15, 2017 at 12:38
  • If list1 is quite long, and you want to omit mentioning it twice within your query, you might consider using a temp table and subqueries. This might also be positive for the performance. Commented Mar 15, 2017 at 13:00
  • @LaurenzAlbe I think the first way is not good and I wanted something like second one Commented Apr 26, 2017 at 4:52

1 Answer 1

8

You can use arrays and the operator <@ (is contained by), e.g.:

with my_table(name1, name2) as (
values ('Emily', 'Bob'), ('Ben', 'Jack'), ('Emily', 'James')
)

select *
from my_table
where array[name1, name2] <@ array['Emily', 'Jack', 'James', 'Chloe'];

 name1 | name2 
-------+-------
 Emily | James
(1 row)

See also: How to use same list twice in WHERE clause?

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.