postgresql - Postgres : Refer to CTE alias within function

标签 postgresql common-table-expression

我想做这样的事情:

...
with blocking_index as (
  (select * from exact_phone_matches) union all (select * from 
   exact_email_matches)
), 
matched_names as (
  select * from get_matches((_rec.name, 'blocking_index')
  )
)
...

即我想将对我的 CTE 别名('blocking_index')的引用传递到一个函数中。该函数包含如下内容:

return query execute format('select * from %s where %s', _table, _whr)

这行不通。我还有其他方法可以做到这一点吗?

最佳答案

您不能使用 CTE 执行此操作,因为 CTE 在定义它的查询之外没有任何意义。

您可以使用临时表。这具有额外的优势,您可以对其进行ANALYZE,以便优化器具有统计信息,而这对于 CTE 是不可能的。

关于postgresql - Postgres : Refer to CTE alias within function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253747/

相关文章:

database - 如何在使用gradle测试之前重建数据库?

postgresql - 在 plpgsql 中使用 CTE

performance - Hive - 如何以最佳性能重用 Hive 中的子查询

sql-server - SQL Server CTE 层次结构关键字搜索

postgresql - 后端 I/O 错误

java - 使用 jdbc 时尝试连接到远程 pg db 时出现奇怪的错误

sql - PostgreSQL 在引用端自动分配外键

sql - 如何将查询的 CTE 输出保存到临时表或表变量中

sql-server - Hive CTE,我可以将值查询为表吗?

node.js - Sequelize - 如何在嵌套关联中使用 where 子句?