簡體   English   中英

在多個查詢中使用 postgres CTE

[英]Use postgres CTE in multiple queries

我可以在這樣的單個查詢中使用 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);

CTE 是一種臨時視圖。 如果您想要一個可以在多個查詢中使用的永久視圖,請改用CREATE VIEW

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM