postgres WITH query声明没有FROM子句

时间:2013-09-13 15:27:32

标签: postgresql with-statement

我编写了一个使用WITH子句的简单查询,但是我收到了这个错误:

  

错误:错误:缺少表“cte”

的FROM子句条目

这是查询,我显然是在放置一个FROM子句。我知道这一定很简单,但我只是没有看到我做错了什么。感谢。

WITH cte AS (
    SELECT cident, "month"
    FROM orders_extended io
    WHERE io.ident = 1    -- 1 will be replaced with a function parameter
)
SELECT *
FROM orders_extended o
WHERE o.cident = cte.cident AND o."month" = cte."month"
ORDER BY o."month" DESC, o.cname

1 个答案:

答案 0 :(得分:1)

信息没有说谎。

WITH cte AS (
    SELECT cident, "month"
    FROM orders_extended io
    WHERE io.ident = 1    -- 1 will be replaced with a function parameter
)
SELECT o.*
FROM orders_extended o
INNER JOIN cte ON (o.cident = cte.cident and o."month" = cte."month")
ORDER BY o."month" DESC, o.cname