【问题标题】:UNION ALL parameterised postgresUNION ALL 参数化 postgres
【发布时间】:2019-10-01 16:47:25
【问题描述】:

我想在 postgres 中做这样的事情

select * from table t where t.one = 11 AND t.two = 12 and t.three = 13
union all
select * from table t where t.one = 21 AND t.two = 22 and t.three = 23

我尝试了join lateralinner joins,但性能太差了。所以我需要union all 这些查询,但我不想只是连接不确定数量的这些值,postgres 是否有类似https://stackoverflow.com/a/55484168/1321514 的东西?

【问题讨论】:

  • “这些值的数量不定”是什么意思?哪些价值观,它们来自哪里,以及它们可以以哪些方式变化?如果他们按照您的意愿行事,也许您应该向我们展示您的加入尝试。
  • 是的,PostgreSQL 有标准的unnest。你试过用吗?

标签: postgresql postgresql-11


【解决方案1】:

我认为根本不需要 UNION。而且我不明白 JOIN 在这里有什么帮助

您的查询相当于:

select * 
from table t 
where (t.one,t.two,t.three) in ( (11,12,13), (21,22,23) );

您也可以尝试加入 VALUES 子句:

select t.* 
from table t 
  join (
     values (11,12,13), (21,22,23) 
  ) as x(c1,c2,c3) on t.one = x.c1 
                  and t.two = x.c2
                  and t.three = x.c3;

【讨论】:

  • 非常感谢@a_horse_with_no_name,这正是我想要的
  • 是否可以使用 java 和 jdbc 来做到这一点?谢谢
  • @otonakav:是的,当然。只需通过Statement.executeQuery()运行该SQL
  • 我的意思是传递包含列one 的值的数组,另一个列two 的数组和另一个列three 的数组,我试图做select * from table where (one, two, three) in (select * from unnest(array[11,21]::integer[], array[12,22]::integer[], array[13,23]::integer[])) 然后在java使用setArrayOf() 设置数组,但这会破坏性能@a_horse_with_no_name
  • 请提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 2014-01-03
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多