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;
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With