sql - 动态 select 为 where 子句使用两个列表
sql - Dynamically select using two lists for the where clause
我有两个变量值列表,但长度相等。
Example:
vals1: a, b, c
vals2: 1, 2, 3
做某事的最佳方法是:
select * from table where (col1=vals1[0] and col2=vals2[0]) or (col1=vals1[1] and col2=vals2[1]) or (col1=vals1[2] and col2=vals2[2])
请记住,列表的长度可以是 1 或更多,并且长度始终相同。如果存在另一个选项(最好以 SQL 查询的形式),我不想循环并构建字符串。如有任何帮助,我们将不胜感激。
unnest
并行数组
select *
from t
where (col1, col2) in (
select (a,b)
from (
select unnest(array['a','b','c']), unnest(array[1,2,3])
) s (a,b)
)
我有两个变量值列表,但长度相等。
Example:
vals1: a, b, c
vals2: 1, 2, 3
做某事的最佳方法是:
select * from table where (col1=vals1[0] and col2=vals2[0]) or (col1=vals1[1] and col2=vals2[1]) or (col1=vals1[2] and col2=vals2[2])
请记住,列表的长度可以是 1 或更多,并且长度始终相同。如果存在另一个选项(最好以 SQL 查询的形式),我不想循环并构建字符串。如有任何帮助,我们将不胜感激。
unnest
并行数组
select *
from t
where (col1, col2) in (
select (a,b)
from (
select unnest(array['a','b','c']), unnest(array[1,2,3])
) s (a,b)
)