作为别名的列在Postgresql中是不明显的

时间:2016-09-28 06:36:21

标签: python django postgresql

我正在将数据库从MySQL迁移到PostgreSQL。在MySQL中我创建了一个这样的视图:

create or replace view translated_attributes_with_attribute_templatevalues as
    select
        concat_ws('',
            translated_attributevalues.attribute_id,
            translated_attributevalues.languagecode,
            attribute_template_value.id
        ) AS id,
        ...
        GROUP BY id

但是在PostgreSQL中我收到了消息:

  

列参考" id"模棱两可的LINE 1:... GROUP BY id

如何使用别名" id"?

我重命名了它,但是代码的其他部分中断了,因为它假定该列被命名为id。

1 个答案:

答案 0 :(得分:1)

重复GROUP BY子句中的表达式:

GROUP BY concat_ws('', ...)

或使用结果列号:

GROUP BY 1

第一个解决方案的唯一优势是它符合SQL标准。