`列“...” PostgreSQL 中视图的指定多次错误

发布于 2024-07-07 13:33:51 字数 392 浏览 13 评论 0原文

我在 PostgreSQL 数据库中有一个大型查询。 查询是这样的:

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

当我作为 sql 查询运行此查询时,它返回所需的行。

但是当我尝试使用相同的查询创建视图时,它返回一个错误:

错误:多次指定列“id”。

(我在执行查询时使用 pgAdminIII。)

我猜会发生这种情况,因为结果集将有多个名为“id”的列。 有没有办法解决这个问题,而无需在查询中写入所有列名?

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

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

When I run this query as a sql query, the it returns the wanted row.

But when I tries to use the same query to create a view, it returns an error:

error: column "id" specified more than once.

(I use pgAdminIII when executing the queries.)

I'll guess this happens because the resultset will have more than one column named "id". Is there someway to solve this, without writing all the column names in the query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

数理化全能战士 2024-07-14 13:33:51

发生这种情况是因为视图将有两个 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

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
溺ぐ爱和你が 2024-07-14 13:33:51

如果只有连接列重复(即具有相同的名称),那么您可以将:更改

select *
from a, b
where a.id = b.id

为:

select *
from a join b using (id)

If only join columns are duplicated (i.e. have the same names), then you can get away with changing:

select *
from a, b
where a.id = b.id

to:

select *
from a join b using (id)
‖放下 2024-07-14 13:33:51

对我来说,这是由于我大量添加列而发生的。
之前版本的create view语句有5列。
我在最终选择中添加了 3 个新列,但在创建视图语句开头的列列表中仅添加了 2 个。
修复列列表后,问题就消失了。
对于我来说,错误消息没有帮助。

For me, it happened due to my mass in adding columns.
There were 5 columns in the previous version of create view statement.
I added 3 new ones in the final select but added only 2 to the list of columns at the create view statement beginning.
After fixing the column list the issue is gone.
As for me error msg is not helpful.

泪眸﹌ 2024-07-14 13:33:51

如果您来到这里是因为您尝试使用像 to_date 这样的函数并收到“已定义多次”错误,请注意您需要为函数使用列别名,例如:

to_date(o.publication_date, 'DD/MM/YYYY') AS publication_date

If you got here because you are trying to use a function like to_date and getting the "defined more than once" error, note that you need to use a column alias for functions, e.g.:

to_date(o.publication_date, 'DD/MM/YYYY') AS publication_date
爱人如己 2024-07-14 13:33:51

我得到了同样的错误如下:

错误:列“?列?” 多次指定

当我尝试创建具有 2 列 'Hello''World' 的视图时,SELECT 语句 如下所示:

CREATE VIEW my_view AS
  SELECT 'Hello', 'World';

因此,我将 TEXT 类型设置为 'Hello' 如下所示,然后我就可以毫无错误地创建视图。 *文档列名称默认为“column”; 另外,列数据类型默认为文本,...

CREATE VIEW my_view AS
  SELECT TEXT 'Hello', 'World';

或者,我使用 VALUES 语句 如下所示,那么我可以创建视图而不会出现错误:

CREATE VIEW my_view AS
  -- SELECT 'Hello', 'World';
  VALUES ('Hello', 'World');

另外,如果您尝试创建具有一列的视图 ('Hello', 'World')< 所示:

CREATE VIEW my_view AS
  SELECT ('Hello', 'World');

/code> 与 SELECT 语句如下 错误如下:

错误:列“行”有伪类型记录

I got the same error below:

ERROR: column "?column?" specified more than once

When I tried to create the view which has 2 columns 'Hello' and 'World' with SELECT statement as shown below:

CREATE VIEW my_view AS
  SELECT 'Hello', 'World';

So, I set TEXT type to 'Hello' as shown below, then I could create the view without error. *The doc says the column name defaults to ?column?; also, the column data type defaults to text, ...:

CREATE VIEW my_view AS
  SELECT TEXT 'Hello', 'World';

Or, I used VALUES statement as shown below, then I could create the view without error:

CREATE VIEW my_view AS
  -- SELECT 'Hello', 'World';
  VALUES ('Hello', 'World');

In addition, If you try to create the view which has one column ('Hello', 'World') with SELECT statement as shown below:

CREATE VIEW my_view AS
  SELECT ('Hello', 'World');

Then, there is the error below:

ERROR: column "row" has pseudo-type record

凉城凉梦凉人心 2024-07-14 13:33:51

语言中没有内置的方法来解决它(坦率地说, * 通常是一种不好的做法,因为它可能会导致随着表模式更改而出现潜在的缺陷 - 您可以执行 table1.*、table2.acolumn、tabl2.bcolumn如果您想要一个表的所有内容并选择性地从另一个表中获取),但如果 PostgreSQL 支持 INFORMATION_SCHEMA,您可以执行以下操作:

DECLARE @sql AS varchar

SELECT @sql = COALESCE(@sql + ', ', '') 
    + '[' + TABLE_NAME + '].[' + COLUMN_NAME + ']'
    + CHAR(13) + CHAR(10)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN ('table1', 'table2')
ORDER BY TABLE_NAME, ORDINAL_POSITION

PRINT @sql

并将结果粘贴到其中以节省大量输入。 当然,您需要手动为具有相同名称的列添加别名。 如果您愿意,您还可以代码生成唯一名称(但我不这样做):

SELECT @sql = COALESCE(@sql + ', ', '') 
    + '[' + TABLE_NAME + '].[' + COLUMN_NAME + '] '
    + 'AS [' + TABLE_NAME + '_' + COLUMN_NAME + ']'
    + CHAR(13) + CHAR(10)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN ('table1', 'table2')
ORDER BY TABLE_NAME, ORDINAL_POSITION

No built-in way in the language to solve it (and frankly, * is a bad practice in general because it can cause latent defects to arise as the table schemas change - you can do table1.*, table2.acolumn, tabl2.bcolumn if you want all of one table and selectively from another), but if PostgreSQL supports INFORMATION_SCHEMA, you can do something like:

DECLARE @sql AS varchar

SELECT @sql = COALESCE(@sql + ', ', '') 
    + '[' + TABLE_NAME + '].[' + COLUMN_NAME + ']'
    + CHAR(13) + CHAR(10)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN ('table1', 'table2')
ORDER BY TABLE_NAME, ORDINAL_POSITION

PRINT @sql

And paste the results in to save a lot of typing. You will need to manually alias the columns which have the same name, of course. You can also code-gen unique names if you like (but I don't):

SELECT @sql = COALESCE(@sql + ', ', '') 
    + '[' + TABLE_NAME + '].[' + COLUMN_NAME + '] '
    + 'AS [' + TABLE_NAME + '_' + COLUMN_NAME + ']'
    + CHAR(13) + CHAR(10)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN ('table1', 'table2')
ORDER BY TABLE_NAME, ORDINAL_POSITION
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文