efl*_*les 13 sql postgresql pgadmin
我在PostgreSQL数据库中有一个大的查询.查询是这样的:
SELECT * FROM table1, table2, ... WHERE table1.id = table2.id...
当我将此查询作为sql查询运行时,它返回所需的行.
但是当我尝试使用相同的查询来创建视图时,它会返回一个错误:
"错误:列"id"指定了多次."
(我在执行查询时使用pgAdminIII.)
我猜这是因为结果集将有多个名为"id"的列.有没有办法解决这个问题,而无需在查询中写入所有列名?
Vin*_*vic 15
发生这种情况是因为视图会有两个id命名列,一个来自table1,另一个来自table2,因为select*.
您需要在视图中指定所需的ID.
SELECT table1.id, column2, column3, ... FROM table1, table2 
WHERE table1.id = table2.id
该查询有效,因为它可以具有同样命名的列...
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
小智 13
如果只复制连接列(即具有相同的名称),那么您可以更改:
select *
from a, b
where a.id = b.id
至:
select *
from a join b using (id)