考虑以下带有两个WITH
clauses
WITH result1 AS (
SELECT ...
),
result2 AS (
INSERT INTO...
)
SELECT <logic dependent on result1>
万一重要,我不需要result2
,但是子句的顺序很重要,因此我无法交换它们(即先计算result2
,然后计算result1
)
问题:如何通过第二子句传递result1
,以便在SELECT
中使用它?
我已经尝试使用RETURNING
clause,但是那个does not work ...
编辑示例:
请考虑以下表格/内容:
CREATE TABLE test(id INT, name VARCHAR)
INSERT INTO test VALUES (1, 'Joe')
然后以下查询给我一个错误:
WITH result1 AS (
SELECT name FROM test WHERE id = 1
), result2 AS (
INSERT INTO test VALUES(2, 'Jack') RETURNING *
) SELECT result1.id == 5 FROM result1
结果:
ERROR: column result1.id does not exist
LINE 8: ) SELECT result1.id == 5 FROM result1
^
SQL state: 42703
Character: 216
我也尝试了... FROM result2
(显然是错误的,因为我需要的是result1
)和... FROM result1, result2
(相同的问题,建议我改用result2.id
) 。
答案 0 :(得分:2)
select count(tag) from building where time > (my timestamp)
答案 1 :(得分:2)
如果您想返回result1
,请修正id
的CTE:
WITH result1 AS (
SELECT id, name
FROM test
WHERE id = 1
)
然后您可以在外部result1.id
中引用SELECT
。