MySQL error: Unknown column in 'where clause'

后端 未结 5 1541
栀梦
栀梦 2020-12-16 17:14

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

I am trying to create a query that will return

5条回答
  •  温柔的废话
    2020-12-16 17:34

    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

提交回复
热议问题