`列“...” PostgreSQL 中视图的指定多次错误
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
发生这种情况是因为视图将有两个 id 命名列,一个来自 table1,一个来自 table2,因为 select *.
您需要指定视图中想要的 id。
该查询有效,因为它可以具有相同名称的列...
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.
The query works because it can have equally named columns...
如果只有连接列重复(即具有相同的名称),那么您可以将:更改
为:
If only join columns are duplicated (i.e. have the same names), then you can get away with changing:
to:
对我来说,这是由于我大量添加列而发生的。
之前版本的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.
如果您来到这里是因为您尝试使用像
to_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.:我得到了同样的错误如下:
当我尝试创建具有 2 列
'Hello'
和'World'
的视图时,SELECT 语句 如下所示:因此,我将
TEXT
类型设置为 'Hello' 如下所示,然后我就可以毫无错误地创建视图。 *文档说列名称默认为“column”; 另外,列数据类型默认为文本,...
:或者,我使用 VALUES 语句 如下所示,那么我可以创建视图而不会出现错误:
另外,如果您尝试创建具有一列的视图
('Hello', 'World')< 所示:
/code> 与 SELECT 语句如下 错误如下:
I got the same error below:
When I tried to create the view which has 2 columns
'Hello'
and'World'
with SELECT statement as shown below:So, I set
TEXT
type to 'Hello' as shown below, then I could create the view without error. *The doc saysthe column name defaults to ?column?; also, the column data type defaults to text, ...
:Or, I used VALUES statement as shown below, then I could create the view without error:
In addition, If you try to create the view which has one column
('Hello', 'World')
with SELECT statement as shown below:Then, there is the error below:
语言中没有内置的方法来解决它(坦率地说, * 通常是一种不好的做法,因为它可能会导致随着表模式更改而出现潜在的缺陷 - 您可以执行 table1.*、table2.acolumn、tabl2.bcolumn如果您想要一个表的所有内容并选择性地从另一个表中获取),但如果 PostgreSQL 支持 INFORMATION_SCHEMA,您可以执行以下操作:
并将结果粘贴到其中以节省大量输入。 当然,您需要手动为具有相同名称的列添加别名。 如果您愿意,您还可以代码生成唯一名称(但我不这样做):
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:
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):