[英]Use postgres CTE in multiple queries
I can use a CTE in a single query like this我可以在这样的单个查询中使用 CTE
with mycte as (...)
insert into table1 (col1) select col1 from mycte where col1 in
(select col1 from mycte)
But what if I want to use mycte
in multiple queries?但是如果我想在多个查询中使用
mycte
怎么办? How can I make something like this work?我怎样才能做这样的工作?
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)
For multiple inserts, you can put them into the same query:对于多个插入,您可以将它们放入同一个查询中:
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);
A CTE is an ad-hoc view. CTE 是一种临时视图。 If you want a permanent view that you can use in multiple queries, then use
CREATE VIEW
instead.如果您想要一个可以在多个查询中使用的永久视图,请改用
CREATE VIEW
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.