SQLAlchemy:如何执行多个独立的CTE?

问题描述 投票:0回答:1

我的工作 SQL 代码有两个 CTE,其中一个必须与主语句并行工作,如下所示:

WITH RECURSIVE cte1 AS (...get initial rows...),
cte2 AS (...select rows from cte1 and insert in table1 in parallel to main statement...)
INSERT INTO table2 (...) SELECT ... FROM cte1;

我在 SQLAlchemy 的帮助下为所有部分编写代码:

cte1 = select(...).cte('cte1', recursive=True)
cte2 = table1.insert().from_select([...], select(...here i use cte1...)).cte('cte2')
result = table2.insert().from_select([...], select(...here i use cte1 too...))

但是我无法在查询中插入 cte2,因为我没有在结果语句中使用它,而且我找不到以其他方式包含 cte2 的方法... 欢迎任何建议。 谢谢。

python sqlalchemy common-table-expression
1个回答
0
投票

添加到

cte2
returning
部分,然后在
result
中使用
cte2
returning
查询数据而不是
cte1

© www.soinside.com 2019 - 2024. All rights reserved.