简体   繁体   English

如何在Postgres中将WHERE语句与OR数组组合使用?

[英]How to use a WHERE statement in Postgres with an array of OR combinations?

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 . 我不确定如何表达这个问题,但是前提是我有一个表,其中主键由两列定义: rowcol 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. 如果我有一个名为id的简单列,我将可以执行WHERE id=ANY($1)这样的子句,其中$1是整数数组。 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. WHERE row=ANY($1) AND col=ANY($2)给了我想要的区域,但没有我所需的确切元组集。 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! 任何见识将不胜感激!

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

要么

where (row,col) in ((1,2),(3,4))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM