PostgreSQL中递归CTE的问题 [英] Issue with recursive CTE in PostgreSQL

查看:186
本文介绍了PostgreSQL中递归CTE的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此查询生成1到4之间的数字。

This query generates the numbers from 1 to 4.

with recursive z(q) as (
  select 1
  union all
  select q + 1 from z where q < 4
  )
select * from z;

但是,如果我对此进行修改,

But, if I modify it to this,

with x as (
  select 1 y
  ),
recursive z(q) as (
  select y from x
  union all
  select q + 1 from z where q < 4
  )
select * from z;

它给出


错误: z处或附近的语法错误

ERROR: syntax error at or near "z"

我在这里做什么错了?

推荐答案

我认为这是因为 RECURSIVE是WITH语句的修饰符,不是普通表表达式 z 的属性,因此可以这样使用它:

I think this is because RECURSIVE is modifier of WITH statement, not a property of common table expression z, so you can use it like this:

with recursive
x as (
  select 1 y
),
z(q) as (
  select y from x
  union all
  select q + 1 from z where q < 4
)
select * from z;

sql小提琴演示

这篇关于PostgreSQL中递归CTE的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆