sql - 在多个查询中使用 postgres CTE
问题描述
我可以像这样在单个查询中使用 CTE
with mycte as (...)
insert into table1 (col1) select col1 from mycte where col1 in
(select col1 from mycte)
但是如果我想mycte
在多个查询中使用呢?我怎样才能使这样的工作?
with mycte as (...)
insert into table1 (col1) select col1 from mycte where col1 in
(select col1 from mycte),
insert into table2 (col1) select col1 from mycte where col1 in
(select col1 from mycte)
解决方案
对于多个插入,您可以将它们放入同一个查询中:
with mycte as (...),
i1 as (
insert into table1 (col1)
select col1
from mycte
where col1 in (select col1 from mycte)
returning *
)
insert into table2 (col1)
select col1
from mycte
where col1 in (select col1 from mycte);
推荐阅读
- .htaccess - 使用 .htaccess 使单词不区分大小写
- c - PAUSE 指令 - RISC-V 中无法识别的操作码“暂停”
- javascript - Javascript为内部函数返回未定义
- python - 反转每个 K 元素子列表
- google-cloud-platform - gcp 云函数批量处理
- python - 如果 __name__=="__main__": 出现错误,我创建了两个文件,其中一个是我编码的 score.py,第二个是 import score.py 的 two.py
- ubuntu-21.04 - 在运行 gdalinfo 时最近升级后在 Ubuntu 21.04 上出现错误
- google-cloud-platform - Google Cloud Storage API:无法生成访问令牌,访问被拒绝
- mysql - 崩溃 MySQL 无法启动 mysql
- angular - 滚动在角离子中消失了