代码之家  ›  专栏  ›  技术社区  ›  Slim

表“A”有一个条目,但不能从查询的这一部分引用它。

  •  1
  • Slim  · 技术社区  · 6 年前

    我编写了一个查询,该查询将从名为 allotment :

    select a.product_id, 
    daterange((lower(a.allotment_period) + concat(n.i - 1, ' days')::interval)::date, 
    (upper(a.allotment_period) + concat(n.i, ' days')::interval)::date, '[]') 
    from test.allotment as a
    cross join(select * from generate_series(1, a.period_length)) as n(i)
    where a.id = 2 
    

    我得到这个错误:

    ERROR: invalid reference to FROM-clause entry for table "a" LINE 5: cross `join(select * from generate_series(1, a.period_length)... ^ HINT: There is an entry for table "a", but it cannot be referenced from this part of the query. SQL state: 42P01 Character: 250`
    

    我发现了一个类似的问题 SQL joins, “there is an entry for table but it cannot be referenced” 但它不能解决我的问题。有人能解决这个问题吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Gordon Linoff    6 年前

    from

    from test.allotment a cross join lateral
         generate_series(1, a.period_length) n(i)
    

    generate_series() select

    from (select a.*, generate_series(1, a.period_length) as i
          from test.allotment a
         ) a