如何将两个with子句连接在一起

时间:2019-07-24 20:41:44

标签: sql

我想将两个with子句表合并在一起

首先带有子句

with t as 
(
    select * from table
),
tt as
( 
     select * from t
)
select * from tt 

加入

第二个有条款

with x as 
(
    select * from table
),
xx as
( 
     select * from x
)
select * from xx

1 个答案:

答案 0 :(得分:0)

您真的不需要使用CTE来别名表,这样您就可以在查询中使用它两次,就像通常使用别名表并将表联接到自身一样。

SELECT * 
FROM 
  table c
  INNER JOIN
  table p
  ON
    p.id = c.parent_id 

当表描述某种层次关系时,我们通常将表连接在一起,其中一行是另一行的父级