View error in PostgreSQL

前端 未结 4 1313
南旧
南旧 2021-01-01 18:41

I have a large query in a PostgreSQL database. The Query is something like this:

SELECT * FROM table1, table2, ... WHERE table1.id = table2.id...
         


        
4条回答
  •  伪装坚强ぢ
    2021-01-01 18:51

    That happens because a view would have two id named columns, one from table1 and one from table2, because of the select *.

    You need to specify which id you want in the view.

    SELECT table1.id, column2, column3, ... FROM table1, table2 
    WHERE table1.id = table2.id
    

    The query works because it can have equally named columns...

    postgres=# select 1 as a, 2 as a;
     a | a
    ---+---
     1 | 2
    (1 row)
    
    postgres=# create view foobar as select 1 as a, 2 as a;
    ERROR:  column "a" duplicated
    postgres=# create view foobar as select 1 as a, 2 as b;
    CREATE VIEW
    

提交回复
热议问题