0

我一生都无法弄清楚这里发生了什么。我希望在以下查询中更新rand_idtable 中的记录 5394560entities以匹配rand_idtable 中相同记录的unique_ids。相反,它会更新到 9?!?!?

mysql> update entities, unique_ids SET entities.rand_id = unique_ids.rand_id where entities.id=5394560;
Query OK, 1 rows affected (2.74 sec)
Rows matched: 1  Changed: 1  Warnings: 0

检查结果:

mysql> select * from entities where id=5394560;
+---------+------------------+---------+---------------------+
| id      | name             | rand_id | created_at          |
+---------+------------------+---------+---------------------+
| 5394560 | Andorra la Vella |       9 | 2013-03-15 13:58:38 |
+---------+------------------+---------+---------------------+
1 row in set (0.00 sec)

mysql> select * from unique_ids where id=5394560;
+---------+----------+
| id      | rand_id  |
+---------+----------+
| 5394560 | 26543652 |
+---------+----------+
1 row in set (0.00 sec)

我在这里错过了一些完全简单和愚蠢的东西吗?!?!?两个表中的两列都在使用int(11),所以我认为数据类型的最大值不是问题,但我可能是错的......

4

1 回答 1

1

您缺少两个表之间的任何连接。基本上实体unique_ids之间的关系 没有定义

尝试添加一些“WHERE entity.something=unique_ids.somethingelse”,其中某物和某物是每个相关表中的列名。

我相信这是您的解决方案(请注意末尾的“AND ...”部分):

update entities, unique_ids 
SET entities.rand_id = unique_ids.rand_id 
where entities.id=5394560 
    AND unique_ids.id=entities.id;
于 2013-03-15T12:23:39.067 回答