【问题标题】:Is there a way to alias a query of the type "table.column" inside a function?有没有办法在函数内为“table.column”类型的查询起别名?
【发布时间】:2020-12-05 23:02:18
【问题描述】:

我创建了一个 CTE,CTE 的第二部分包含一个带有 st_contains 的选择。在这一个中,我有两列具有相同名称的不同表。我想为这个“table.column”组合之一加上别名,因为当我在 CTE 末尾进行选择时会输出错误;列引用“ ”不明确。

with
   table1 as (
   ...
   ),         
   table2 as (
   select*
      from table3, table4
      where st_contains (table3.atribute1, table4.atribute1)
)
select
   table1.atribute1      
   table2.atribute1      #here i need somethig like table2.table3.atribute1
   from table1
   join table2 on table1.atribute2=table2.atribute2
;

我希望我能很好地解释这个问题。 谢谢!

【问题讨论】:

  • 您的 with 子句必须包含一个或多个有效的 sql 语句。您的第二个缺少选择列表。也没有开放的父母。 table2 as ( select table3.attribute1 AS whatever FROM ...)

标签: postgresql postgis common-table-expression


【解决方案1】:

为 table2 CTE 中的 table3 和 table4 列起别名以解决歧义。

with
   table1 as (
   ...
   ),         
   table2 as (
   select table3.attribute1 table3atrr1, table4.attribute1 table4attr1
      from table3, table4
      where st_contains (table3.atribute1, table4.atribute1)
)
select
   table1.atribute1      
   table2.table3atrr1 -- use the aliased column name
   from table1
   join table2 on table1.atribute2=table2.atribute2
;

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多