【问题标题】:There is an entry for table "a", but it cannot be referenced from this part of the query表“a”有一个条目,但无法从查询的这一部分引用
【发布时间】:2019-01-06 15:38:31
【问题描述】:

我编写了一个查询,它将从名为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”,但它并没有解决我的问题。有人有解决这个问题的办法吗?

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    问题出在from 子句中。在更新的 Postgres 版本中,您可以使用:

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

    在旧版本中,您可以在select 中包含generate_series()

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

    【讨论】:

      猜你喜欢
      • 2015-07-13
      • 2019-01-14
      • 1970-01-01
      • 2016-06-19
      • 2014-08-08
      • 2018-11-17
      • 2019-11-04
      • 1970-01-01
      • 2016-12-09
      相关资源
      最近更新 更多