0

I'm not sure how to phrase this question, but the premise is I have a table where the primary key is defined by two columns: row and col. I also want to query for many individual columns, which is where my problem comes into play.

If I had a simple column named id, I would be able to do a clause such as WHERE id=ANY($1) where $1 is an array of integers. However, with a primary key consisting of two columns, I wouldn't be able to the same.

WHERE row=ANY($1) AND col=ANY($2) gives me a region of what I want, but not the exact set of tuples that I need. Right now I'm generating a template query string with many conditions, such as:

WHERE row=$1 AND col=$2 OR
      row=$3 AND col=$4 OR ... 

How can I avoid generating this "query template"? I don't think this is a very elegant solution, but it's the solution I have right now. Any insight would be appreciated!

2
  • 1
    Can't you do WHERE ARRAY[row,col] = ANY($1) where $1 is an array of arrays of integers? Commented Sep 23, 2017 at 23:46
  • Is that a thing? If so, I'm gonna try it out! Commented Sep 23, 2017 at 23:47

1 Answer 1

1
where (row,col) = any(array[(1,2),(3,4)])

or

where (row,col) in ((1,2),(3,4))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, was having a rough time with the syntax. I've used Postgres quite a bit, but not enough to know the ins and outs.

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.