SQL - 引用列的提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37910287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL - HINT to reference a column
提问by philippos
I am very beginner at SQL, so I am sorry if this question is primitive.
I just started following the tutorial of http://www.w3schools.com, so downloaded the "Northwind" database to try working on it, and using the pgAdmin 3 console to access the DB.
I just tried simple command to choose one column of the table, but it gives the same following message with any column from any table
我是 SQL 的初学者,所以如果这个问题很原始,我很抱歉。
我刚开始学习http://www.w3schools.com的教程,所以下载了“Northwind”数据库来尝试处理它,并使用 pgAdmin 3 控制台访问数据库。我只是尝试了简单的命令来选择表的一列,但它给出了与任何表中的任何列相同的以下消息
LINE 1: select City from Customers;
^
HINT: Perhaps you meant to reference the column "customers.City".
I would like to ask is there any thing wrong in my command? and how to fix it?
我想问一下我的命令有什么问题吗?以及如何解决它?
Thanks
谢谢
回答by Tometzky
When you imported this "Northwind" database column names were imported in CamelCase - your import must've added double quotes to column identifiers to create table
queries.
当您导入此“Northwind”数据库列名称时,它是在 CamelCase 中导入的 - 您的导入必须已将双引号添加到create table
查询的列标识符中。
This is rather unfortunate, as this would cause that you'd have to quote them also in all queries, like:
这是相当不幸的,因为这将导致您必须在所有查询中也引用它们,例如:
select "City" from customers;
To remain sane I'd suggest you to rename all columns to lower case. This way it wouldn't matter what case you use, as Postgres converts all unquoted identifiers to lower case automatically. Then any of this would work:
为了保持理智,我建议您将所有列重命名为小写。这样,您使用哪种大小写无关紧要,因为 Postgres 会自动将所有未加引号的标识符转换为小写。然后任何这些都可以工作:
select city from customers;
select City from Customers;
SELECT CITY FROM CUSTOMERS;
回答by Robert Columbia
There is nothing wrong with your query. It looks like your tutorial wants you to follow the practice of always prepending the table name to columns you reference. In reality, this is only required when it would otherwise cause an ambiguity (e.g. if you are referencing two tables, both of which have a City column).
您的查询没有任何问题。看起来您的教程希望您遵循始终将表名添加到您引用的列的做法。实际上,只有在否则会导致歧义时才需要这样做(例如,如果您正在引用两个表,而这两个表都有一个 City 列)。