【问题标题】:ERROR 1054 (42S22): Unknown column 'year' in 'where clause'错误 1054 (42S22):“where 子句”中的未知列“年份”
【发布时间】:2018-10-13 00:43:03
【问题描述】:

不确定我的列名是怎么回事。

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

这是表限制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                         

【问题讨论】:

  • 你能在列上试试反勾号吗?
  • `独立年份`
  • MySQL 将year-independence 解释为算术表达式,因此它正在寻找名为yearindependence 的列。您需要使用反引号转义 year-independence 以防止这种情况发生。
  • 另一个名称不佳的列导致问题的示例。使用真实的列名,并使它们在您的 UI 中漂亮。一个很好的例子:yearindependence 作为列名。当需要显示时,使用SELECT yearindependence as "Year Of Independence"

标签: mysql sql mysql-error-1054


【解决方案1】:

MySQL 中的某些对象,包括数据库、表、索引、 列、别名、视图、存储过程、分区、表空间、资源 组和其他对象名称称为标识符。 [...] 如果 标识符包含特殊字符或者是保留字,您必须 每当你提到它时引用它。 [...]

根据https://dev.mysql.com/doc/refman/8.0/en/identifiers.html

标识符引号字符是反引号(`):

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

你的情况

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

您可能已经注意到我用反引号包围了所有标识符,因为这样做是个好习惯。

【讨论】:

    【解决方案2】:

    试试:

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

    【讨论】:

    • 嗨@ChrisKehl,如果你能选择正确的答案,我会很高兴,因为这是我第一次回答!很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2020-06-08
    相关资源
    最近更新 更多