My SQL query contains a WHERE
clause looking like this:
WHERE
name1 in ('Emily', 'Jack', 'James', 'Chloe')
OR name2 in ('Emily', 'Jack', 'James', 'Chloe')
Note that the same list appears twice, which is quite unsatisfying (and my real list is actually longer).
What would be a better way to write this query?
You can use arrays and overlap operator &&
, e.g.:
with my_table(name1, name2) as (
values ('Emily', 'Bob'), ('Ben', 'Jack'), ('Bob', 'Ben')
)
select *
from my_table
where array[name1, name2] && array['Emily', 'Jack', 'James', 'Chloe'];
name1 | name2
-------+-------
Emily | Bob
Ben | Jack
(2 rows)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments