Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres sql select combination of two columns

Tags:

sql

A user can choose a list of option combinations and then search for them.

The sql could look like this.

select * from p where (option_type = 'X' and value = 'A') 
or (option_type = 'X' and value = 'B')
or (option_type = 'Y' and value = 'D')

But of course I do not want to have n number of or's

How would a good sql look like that performs ??? The user can choose many option combinations.

Thanks.

like image 946
klind Avatar asked Sep 14 '25 05:09

klind


1 Answers

No need for multiple ORs:

select * 
from p 
where (option_type, value) in ( ('X' ,'A'), ('X','B'), ('Y','D') )