Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 带有查询声明no FROM子句的postgres_Postgresql_With Statement - Fatal编程技术网

Postgresql 带有查询声明no FROM子句的postgres

Postgresql 带有查询声明no FROM子句的postgres,postgresql,with-statement,Postgresql,With Statement,我编写了一个使用WITH子句的简单查询,但我遇到了以下错误: 错误:错误:表cte的子句条目中缺少 这是一个问题,我很清楚地在这里放了一个FROM子句。我知道这一定很简单,但我只是不知道我做错了什么。谢谢 WITH cte AS ( SELECT cident, "month" FROM orders_extended io WHERE io.ident = 1 -- 1 will be replaced with a function parameter ) SE

我编写了一个使用WITH子句的简单查询,但我遇到了以下错误:

错误:错误:表cte的子句条目中缺少

这是一个问题,我很清楚地在这里放了一个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

这个消息没有撒谎

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

别名cte显然不在FROM子句中…谢谢。我错误地认为使用WITH这样的函数实际上会将cte放入查询中。我没有意识到我需要显式地加入它,当然,一旦我看到它,这是有意义的。你不必加入到CTE,你也可以在WHERE子句中引用它,类似于你所做的,但是通过子查询。例如,其中o.cident=从cte限制1中选择cident。此答案不完整,可以通过添加代码如何解决问题的解释来改进。此外,代码示例可以简化为与op的原始代码示例不同的部分。