mysql 更新查询错误代码 1054 字段列表中的未知列

nob*_*alo 4 mysql update errors

Error: Code 1054. Unknown column 'U2.id_naslov' in 'field list' 在 MySQL Workbench 中抛出这个简单的查询:

UPDATE krneki_1 AS U1, krneki_2 AS U2 
SET U1.id_naslov = U2.id_naslov
WHERE (U2.id_zaposlen = U1.id_naslovi_zaposleni)
Run Code Online (Sandbox Code Playgroud)

我在网上搜索并阅读了其他帖子,但没有任何帮助...

我想这是一个微不足道的解决方案,但我看不到它。

这种错误从未出现在 TSQL (sql server) 上。

表 krneki_2 是由 Mysql 工作台通过数据导入(创建新表)稍后在发生此错误时创建的,我还将数字字段更改为 smallint 只是为了看看它是否有帮助......但是......没有。

结果SHOW CREATE TABLE krneki_2

       Table: krneki_2 
Create Table: CREATE TABLE `krneki_2` 
( `id` smallint(6) NOT NULL AUTO_INCREMENT, 
  `id_naslov` smallint(6) NOT NULL, 
  `id_zaposlen` smallint(6) NOT NULL, 
  PRIMARY KEY (id) 
) ENGINE=InnoDB AUTO_INCREMENT=204 DEFAULT CHARSET=utf8 

1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

结果SHOW CREATE TABLE krneki_1

       Table: krneki_1
Create Table: CREATE TABLE `krneki_1` (
  `id_naslovi_zaposleni` smallint(6) NOT NULL AUTO_INCREMENT,
  `id_naslov` smallint(6) DEFAULT NULL,
  `id_zaposleni` smallint(6) DEFAULT NULL,
  `id_aktiven` tinyint(4) DEFAULT '0',
  `cas_vnosa` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_naslovi_zaposleni`)
) ENGINE=InnoDB AUTO_INCREMENT=256 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

结果来自information_schema,特别是来自评论中建议的此查询:

select
  table_catalog, table_schema, table_name, column_name, ordinal_position
from information_schema.columns
where table_name like '%krneki_1%' and column_name like '%naslov%' ;
Run Code Online (Sandbox Code Playgroud)

krneki_1和 的结果naslov

+---------------+--------------+-------------+----------------------+------------------+
| table_catalog | table_schema | table_name  | column_name          | ordinal_position |
+---------------+--------------+-------------+----------------------+------------------+
| def           | hq_db        | krneki_1    | id_naslovi_zaposleni |                1 |
| def           | hq_db        | krneki_1    | id_naslov            |                2 |
+---------------+--------------+-------------+----------------------+------------------+
2 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

krneki_2和 的结果naslov

+---------------+--------------+-------------+--------------+------------------+
| table_catalog | table_schema | table_name  | column_name  | ordinal_position |
+---------------+--------------+-------------+--------------+------------------+
| def           | hq_db        | krneki_2    | id_naslov    |                2 |
+---------------+--------------+-------------+--------------+------------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

krneki_2和 的结果zaposlen

+---------------+--------------+-------------+--------------+------------------+
| table_catalog | table_schema | table_name  | column_name  | ordinal_position |
+---------------+--------------+-------------+--------------+------------------+
| def           | hq_db        | krneki_2    | id_zaposlen  |                3 |
+---------------+--------------+-------------+--------------+------------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

进一步挖掘,如建议:

select 
  table_catalog, table_schema, table_name, column_name, ordinal_position,         
  char_length(column_name) as cl, length(column_name) as l
from information_schema.columns 
where table_name = 'krneki_2' ;
Run Code Online (Sandbox Code Playgroud)

结果krneki_2

+-------------+------------+----------+-----------+----------------+---+---+-------------+
|table_catalog|table_schema|table_name|column_name|ordinal_position| cl| l | column_type |
+-------------+------------+----------+-----------+----------------+---+---+-------------+
| def         | hq_db      | krneki_2 |id         |              1 |  2|  2| smallint(6) |
| def         | hq_db      | krneki_2 |id_naslov  |              2 | 10| 12| smallint(6) |
| def         | hq_db      | krneki_2 |id_zaposlen|              3 | 11| 11| smallint(6) |
+-------------+------------+----------+-----------+----------------+---+---+-------------+
3 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

结果krneki_1

+-------------+------------+----------+--------------------+----------------+--+--+-----------+
|table_catalog|table_schema|table_name| column_name        |ordinal_position|cl| l|column_type|
+-------------+------------+----------+--------------------+----------------+--+--+-----------+
| def         | hq_db      | krneki_1 |id_naslovi_zaposleni|              1 |20|20|smallint(6)|
| def         | hq_db      | krneki_1 |id_naslov           |              2 | 9| 9|smallint(6)|
| def         | hq_db      | krneki_1 |id_zaposleni        |              3 |12|12|smallint(6)|
| def         | hq_db      | krneki_1 |id_aktiven          |              4 |10|10|tinyint(4) |
| def         | hq_db      | krneki_1 |cas_vnosa           |              5 | 9| 9|datetime   |
+-------------+------------+----------+--------------------+----------------+--+--+-----------+
5 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

krneki_2 与十六进制:

+-------------+------------+----------+-----------+----------------+--+--+-------------------------+
|table_catalog|table_schema|table_name|column_name|ordinal_position|cl|l | hex                     |      
+-------------+------------+----------+-----------+----------------+--+--+-------------------------+ 
| def         | hq_db      | krneki_2 |id         |              1 | 2| 2|6964                     |   
| def         | hq_db      | krneki_2 |id_naslov  |              2 |10|12|EFBBBF69645F6E61736C6F76 |   
| def         | hq_db      | krneki_2 |id_zaposlen|              3 |11|11|69645F7A61706F736C656E   |  
+-------------+------------+----------+-----------+----------------+--+--+-------------------------+   
3 rows in set (0.00 sec)  
Run Code Online (Sandbox Code Playgroud)

krneki_1 与十六进制:

+-------------+------------+----------+--------------------+----------------+--+--+----------------------------------------+
|table_catalog|table_schema|table_name|column_name         |ordinal_position|cl| l|hex                                     |              
+-------------+------------+----------+--------------------+----------------+--+--+----------------------------------------+
| def         | hq_db      | krneki_1 |id_naslovi_zaposleni|              1 |20|20|69645F6E61736C6F76695F7A61706F736C656E69|
| def         | hq_db      | krneki_1 |id_naslov           |              2 | 9| 9|69645F6E61736C6F76                      |
| def         | hq_db      | krneki_1 |id_zaposleni        |              3 |12|12|69645F7A61706F736C656E69                |
| def         | hq_db      | krneki_1 |id_aktiven          |              4 |10|10|69645F616B746976656E                    |
| def         | hq_db      | krneki_1 |cas_vnosa           |              5 | 9| 9|6361735F766E6F7361                      |
+-------------+------------+----------+--------------------+----------------+--+--+----------------------------------------+
5 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

ype*_*eᵀᴹ 8

错误信息非常清楚。该表krneki_2没有名为 的列id_naslov。除非系统表中存在某些损坏或错误,否则毫无疑问。

因此,我们必须消除出现这种情况的几种可能性:


  1. CREATE TABLE语句和之间存在不匹配UPDATE

    CREATE TABLE ` krneki_1` ...
    
    CREATE TABLE ` krneki_2` ...
    
    UPDATE krneki_1 AS U1, krneki_2 AS U2 ...
    
    Run Code Online (Sandbox Code Playgroud)

不匹配是名称开头的空格。

这应该会给出“表'krneki_1'不存在”的错误,所以我有根据的猜测是你有两个版本的表krneki_1,没有空格的版本没有id_naslov列。

我们消除了这种可能性,这是来自 OP 的复制粘贴错误。


  1. 在列名称CREATE TABLEUPDATE不相同。它们可能看起来相同但可能存在不可打印的字符,或者它们可能具有看起来相同但代码点不同的 Unicode 字符。为了找出答案,我们可以使用这个查询:

    select 
      table_catalog, table_schema, table_name, column_name, ordinal_position,
      char_length(column_name) as cl, length(column_name) as l,
      hex(column_name) as hex
    from information_schema.columns 
    where table_name = 'krneki_2' ;
    
    Run Code Online (Sandbox Code Playgroud)

这揭示了差异(不需要从输出中删除的列):

+------------+-------------+------------------+----+----+
| table_name | column_name | ordinal_position | cl | l  |
+------------+-------------+------------------+----+----+
| krneki_2   | id          |                1 |  2 |  2 |
| krneki_2   | id_naslov   |                2 | 10 | 12 |   -- !!! --
| krneki_2   | id_zaposlen |                3 | 11 | 11 |     
+------------+-------------+------------------+----+----+
Run Code Online (Sandbox Code Playgroud)

Note 12 大于 10 并且有问题!这意味着列名有 10 个字符并使用 12 个字节。这些数字都应该是 9(如果我们计算id_naslov正确并且所有 9 个字符都是 ASCII),那么那里就会发生一些可疑的事情。

您可以hex(column_name)在最后一个查询的选择列表中添加,我们就会知道列名到底是什么。然后您可以将其更改为仅具有可打印的 ascii 字符。

要修复,您需要这样的东西:

set @column := X'EFBBBF69645F6E61736C6F76' ;  
set @qry:= concat('alter table krneki_2 change column ', 
                  @column, 
                  ' id_naslov smallint') ;
prepare stmt from @qry ;
execute stmt ;
Run Code Online (Sandbox Code Playgroud)