Vinay
Vinay

Reputation: 334

How to fetch data based on multiple key pair in Postgresql + Rails4

I have a table XYZ which has few columns in which, I need to filter out resultset based on Two columns (col1, col2) requested params

Request params like:

filter: [ {col1: val1, col2: val2 }, {col1: val11, col2: val12 }]

API request received a params which contains Array of Hash, each hash has pair which is a requested query filter. Here is my table structure and expected resultset

Table

id      col1    col2
----------------------
1       val1    val2
2       val11   val12
3       val21   val12
4       val1    val2
5       val33   val33

Result should be

id      col1    col2
--------------------
1       val1    val2
2       val11   val12
4       val1    val2

I have checked https://dba.stackexchange.com/questions/117767/postgresql-pattern-match-against-array-elements but don't understand. Please help

Upvotes: 0

Views: 151

Answers (1)

user330315
user330315

Reputation:

You can use an IN condition based on the values provided:

select *
from xyz
where (col1, col2) in ( ('val1', 'val2'), ('vall11', 'vall12') ) 

Upvotes: 1

Related Questions