Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

sql - MySQL error: Unknown column in 'where clause'

I have a table called bank with three columns: 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:

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

Can anyone see what I am doing wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...