我想在我的查询中使用多个 CTE,但我无法让它工作。以下是我想做的一个例子:
WITH tbl1 AS
(SELECT *
FROM tblStuff)
WITH tbl2 AS
(SELECT tbl1.fldStuff1 ...
FROM tbl1, tblStuff2)
SELECT *
FROM tbl2
我想在我的查询中使用多个 CTE,但我无法让它工作。以下是我想做的一个例子:
WITH tbl1 AS
(SELECT *
FROM tblStuff)
WITH tbl2 AS
(SELECT tbl1.fldStuff1 ...
FROM tbl1, tblStuff2)
SELECT *
FROM tbl2
您只需指定WITH
一次。然后,您可以列出用逗号分隔的其他 CTE。例如:
WITH cte1 AS
(
...
),
cte2 AS
(
...
)
SELECT ...;
WITH tbl1 AS
(SELECT *
FROM tblStuff),
tbl2 AS
(SELECT tbl1.fldStuff1, tbl1.fldStuff2, tblStuff2.fldStuff1, tblStuff2.fldStuff2
FROM tbl1, tblStuff2)
SELECT *
FROM tbl2
当您使用多个 CTE 时,不需要额外的 WITH,用逗号分隔。