3

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

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

但是,如果我将其修改为此,

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”处或附近的语法错误

我在这里做错了什么?

4

1 回答 1

4

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

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 fiddle demo

于 2013-10-17T07:22:05.023 回答