Search code examples
mysqlsqlmysql-error-1054

ERROR 1054 (42S22): Unknown column 'year' in 'where clause'


Not sure what's going on with my column name.

mysql> SELECT fullname FROM country WHERE year-independence > 1945;
ERROR 1054 (42S22): Unknown column 'year' in 'where clause'

here is the table Limit 5:

+----------------------------------------------+
| code | fullname             | continent     | region                    | area   | year-independence | population | avg-lifespan | avg-GNP   | form-government                              |
+------+----------------------+---------------+---------------------------+--------+-------------------+------------+--------------+-----------+----------------------------------------------+
| ABW  | Aruba                | North America | Caribbean                 |    193 |                 0 |     103000 |        78.40 |    828.00 | Nonmetropolitan Territory of The Netherlands |
| AFG  | Afghanistan          | Asia          | Southern and Central Asia | 652090 |              1919 |   22720000 |        45.90 |   5976.00 | Islamic Emirate                              |
| AGO  | Angola               | Africa        | Central Africa            | 124670 |              1975 |   12878000 |        38.30 |   6648.00 | Republic                                     |
| AIA  | Anguilla             | North America | Caribbean                 |     96 |                 0 |       8000 |        76.10 |     63.20 | Dependent Territory of the UK                |
| ALB  | Albania              | Europe        | Southern Europe           |  28748 |              1912 |    3401200 |        71.60 |   3205.00 | Republic                         

Solution

  • Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, resource group and other object names are known as identifiers. [...] If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. [...]

    as per https://dev.mysql.com/doc/refman/8.0/en/identifiers.html

    The identifier quote character is the backtick (`):

    mysql> SELECT * FROM `select` WHERE `select`.id > 100;
    

    In your case

    mysql> SELECT `fullname` FROM `country` WHERE `year-independence` > 1945;
    

    You might've noticed I surrounded all the identifiers with backticks as it's good habit to do that.