有人可以帮我指出这个简单的 WHERE 子句有什么问题吗?

Can someone help point out to me what is wrong with this no brainer WHERE clause?

这很简单,但不知何故,我在我的数据库中对这个查询做错了。

我有以下查询:

SELECT login FROM accounts WHERE login = "loginname";

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

column "loginname" does not exist

这很简单,为什么这个查询不能正常工作?我有一个登录列,我知道这个用户存在,因为我发现这个人使用 rails 控制台。为什么登录条件将其自身称为列?

如果要匹配字符串,请尝试使用单引号 ''

SELECT login FROM accounts WHERE login = 'loginname';

勾选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.

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

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

如果您 believe the documentation,似乎“ ”是问题所在。字符串值需要单引号。

来自PostgreSQL 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. The example can be written with quoted identifiers like this:

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.

因此在您的查询中 "loginname" 与不带引号的 loginname 相同 - 它试图引用具有该名称的列。要使其成为文字字符串,请改用单引号。