有人能不能帮我指出这个不需要动脑筋的地方条款有什么问题?

时间:2021-11-23 16:46:11

This is very simple but somehow I'm doing something wrong with this query on my database.

这很简单,但是我对数据库上的查询做了一些错误的处理。

I have this query below:

我有以下问题:

SELECT login FROM accounts WHERE login = "loginname";

When I execute this query the result I get is this:

当我执行这个查询时,得到的结果是:

column "loginname" does not exist

This is a no brainer, why is this query not working properly? I have a login column and I know that this user exists because I've found this person with the rails console. Why is the login criteria referring to itself as a column?

这毫无疑问,为什么这个查询不能正常工作?我有一个登录列,我知道这个用户存在,因为我在rails控制台找到了这个人。为什么登录条件将自己作为列引用?

4 个解决方案

#1


4  

Try with single quotes '' if you are trying to match a string

如果你想匹配一个字符串,可以使用单引号

SELECT login FROM accounts WHERE login = 'loginname';

Check the documentation

检查文档

There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named "select", whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected.

还有第二种标识符:分隔标识符或引用标识符。它通过在双引号(")中封装任意字符序列而形成。分隔符始终是标识符,而不是关键字。因此,“select”可以用于引用名为“select”的列或表,而未引用的select将作为关键字,因此在使用表或列名称时将引发解析错误。

#2


2  

Double quotes (") are used to refer to object names, in a case sensitive way. In this case, "loginname" is interpreted as a column name, and the query fails, since there is no such column. In order to refer to a string literal, you should use single quotes ('):

双引号(")用于以区分大小写的方式引用对象名。在这种情况下,“loginname”被解释为一个列名,查询失败,因为没有这样的列。为了引用字符串文字,您应该使用单引号('):

SELECT login FROM accounts WHERE login = 'loginname';
-- Here ---------------------------------^---------^

#3


1  

It seems that the " " are the problem if you believe the documentation. Single quotes are required for string values.

如果您相信文档,那么“”似乎就是问题所在。字符串值需要单引号。

#4


0  

From the PostgreSQL Documentation:

PostgreSQL的文档:

There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named "select", whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected. The example can be written with quoted identifiers like this:

还有第二种标识符:分隔标识符或引用标识符。它通过在双引号(")中封装任意字符序列而形成。分隔符始终是标识符,而不是关键字。因此,“select”可以用来引用一个名为“select”的列或表,而未引用的select将被当作一个关键字,因此当使用表或列名时,会引发解析错误。该示例可以使用如下所示的引用标识符编写:

UPDATE "my_table" SET "a" = 5;

Quoted identifiers can contain any character, except the character with code zero. (To include a double quote, write two double quotes.) This allows constructing table or column names that would otherwise not be possible, such as ones containing spaces or ampersands. The length limitation still applies.

引用标识符可以包含任何字符,但代码为零的字符除外。(要包含双引号,请写双引号。)这允许构造否则不可能实现的表或列名,例如包含空格或与号的表或列名。长度限制仍然适用。

So in your query "loginname" is the same as having loginname without quotes - it's attempting to refer to a column with that name. To make it a literal string, use single-quotes instead.

所以在您的查询中,“loginname”等同于没有引号的loginname—它试图引用具有该名称的列。要使它成为文字字符串,请使用单引号代替。

#1


4  

Try with single quotes '' if you are trying to match a string

如果你想匹配一个字符串,可以使用单引号

SELECT login FROM accounts WHERE login = 'loginname';

Check the documentation

检查文档

There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named "select", whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected.

还有第二种标识符:分隔标识符或引用标识符。它通过在双引号(")中封装任意字符序列而形成。分隔符始终是标识符,而不是关键字。因此,“select”可以用于引用名为“select”的列或表,而未引用的select将作为关键字,因此在使用表或列名称时将引发解析错误。

#2


2  

Double quotes (") are used to refer to object names, in a case sensitive way. In this case, "loginname" is interpreted as a column name, and the query fails, since there is no such column. In order to refer to a string literal, you should use single quotes ('):

双引号(")用于以区分大小写的方式引用对象名。在这种情况下,“loginname”被解释为一个列名,查询失败,因为没有这样的列。为了引用字符串文字,您应该使用单引号('):

SELECT login FROM accounts WHERE login = 'loginname';
-- Here ---------------------------------^---------^

#3


1  

It seems that the " " are the problem if you believe the documentation. Single quotes are required for string values.

如果您相信文档,那么“”似乎就是问题所在。字符串值需要单引号。

#4


0  

From the PostgreSQL Documentation:

PostgreSQL的文档:

There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named "select", whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected. The example can be written with quoted identifiers like this:

还有第二种标识符:分隔标识符或引用标识符。它通过在双引号(")中封装任意字符序列而形成。分隔符始终是标识符,而不是关键字。因此,“select”可以用来引用一个名为“select”的列或表,而未引用的select将被当作一个关键字,因此当使用表或列名时,会引发解析错误。该示例可以使用如下所示的引用标识符编写:

UPDATE "my_table" SET "a" = 5;

Quoted identifiers can contain any character, except the character with code zero. (To include a double quote, write two double quotes.) This allows constructing table or column names that would otherwise not be possible, such as ones containing spaces or ampersands. The length limitation still applies.

引用标识符可以包含任何字符,但代码为零的字符除外。(要包含双引号,请写双引号。)这允许构造否则不可能实现的表或列名,例如包含空格或与号的表或列名。长度限制仍然适用。

So in your query "loginname" is the same as having loginname without quotes - it's attempting to refer to a column with that name. To make it a literal string, use single-quotes instead.

所以在您的查询中,“loginname”等同于没有引号的loginname—它试图引用具有该名称的列。要使它成为文字字符串,请使用单引号代替。