MySQL 错误:“where 子句”中的未知列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10142583/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:57:06  来源:igfitidea点击:

MySQL error: Unknown column in 'where clause'

mysqlsqlselect

提问by Markum

I have a table called bankwith three columns: uid, nick, balance.

我有一个名为bank三列的表:uid, nick, balance

I am trying to create a query that will return the balance based on the nick, and I am getting an error Unknown column 'Alex' in 'where clause'when I use this query:

我正在尝试创建一个查询,该查询将根据昵称返回余额,但Unknown column 'Alex' in 'where clause'在使用此查询时出现错误:

SELECT b.balance FROM bank AS b WHERE b.nick=`Alex` LIMIT 1

Can anyone see what I am doing wrong here?

谁能看到我在这里做错了什么?

回答by Frambot

backticks (`) are used for identifiers, like table names, column names, etc. Single quotes(') are used for string literals.

反引号 (`) 用于标识符,如表名、列名等。单引号 (') 用于字符串文字。

You want to do:

你想做:

SELECT b.balance FROM bank AS b WHERE b.nick='Alex' LIMIT 1

Or, to be more explicit:

或者,更明确地说:

SELECT `b`.`balance` FROM `bank` AS b WHERE `b`.`nick`='Alex' LIMIT 1

When there is no chance of ambiguity, and when table/column names do not have special characters or spaces, then you can leave the ` off.

如果没有歧义,并且表/列名称没有特殊字符或空格,则可以将 ` 关闭。

Here is some documentation that is dry and hard to read: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

以下是一些枯燥且难以阅读的文档:http: //dev.mysql.com/doc/refman/5.0/en/identifiers.html

But here is a related question on dba.stackoverflow that is easier to read: https://dba.stackexchange.com/questions/23129/benefits-of-using-backtick-in-mysql-queries

但这里有一个关于 dba.stackoverflow 的相关问题,它更容易阅读:https://dba.stackexchange.com/questions/23129/benefits-of-using-backtick-in-mysql-queries

And here is a very good page that I recommend everyone read: http://www.sitepoint.com/forums/showthread.php?408497-the-big-bad-thread-of-quot-MySQL-Best-Practices-and-Other-Useful-Information-quot

这是一个非常好的页面,我建议大家阅读:http: //www.sitepoint.com/forums/showthread.php?408497-the-big-bad-thread-of-quot-MySQL-Best-Practices-and -其他有用的信息-quot

回答by fancyPants

You are using the wrong "`"

您使用了错误的“`”

Use ' instead

使用 ' 代替

SELECT b.balance FROM bank AS b WHERE b.nick='Alex' LIMIT 1

回答by mellamokb

You need to use single-quote ('), not tick marks for values of your fields

您需要使用单引号 ( '),而不是字段值的刻度线

SELECT b.balance FROM bank AS b WHERE b.nick='Alex' LIMIT 1

Tick marks are used to denote field names.

刻度线用于表示字段名称。

回答by Guilherme Macedo

This topic helps me a lot. The SQLwas causing the error because the variables weren't with like it: select * from accounts where name='$variable'in where. But it happened when only when I add more than one condition in WHERE.

这个话题对我帮助很大。该SQL是造成错误,因为变量是不喜欢它:select * from accounts where name='$variable'在哪里。但只有当我在WHERE.

回答by Your Common Sense

Another reason for such an error is, well, there is no such columnin the given table. Check spelling, letter case, typographic mistakes and the actual presence of the given field in the table definition.

这种错误的另一个原因是,给定的表中没有这样的列。检查拼写、字母大小写、印刷错误以及表定义中给定字段的实际存在。