15

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?

1
  • Did you use backticks around "Alex" or are those singlequotes?
    – gmalette
    Commented Apr 13, 2012 at 14:22

4 Answers 4

36

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

6

You are using the wrong "`"

Use ' instead

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

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.

-4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.