可以复制列'哈希'从TABLE2到列' hash'在TABLE1?
谢谢你的帮助! 亲切的问候, 保罗
TABLE 1
+----+--------+----------+
| id | name | hash |
+----+--------+----------+
| 21 | John | |
| 22 | Ann | |
| 23 | Peter | |
+----+--------+----------+
TABLE 2
+----------+
| hash |
+----------+
| vfrtycfg |
| gtytghjg |
+----------+
我需要得到这个结果:
+----+--------+----------+
| id | name | hash |
+----+--------+----------+
| 21 | John | vfrtycfg |
| 22 | Ann | gtytghjg |
| 23 | Peter | |
+----+--------+----------+
答案 0 :(得分:0)
UPDATE table1 AS t1
JOIN (SELECT @rownum AS rownum, hash, @rownum := @rownum + 1
FROM table2
CROSS JOIN (SELECT @rownum := MIN(id) FROM table1) AS var) AS t2
ON t1.id = t2.rownum
SET t1.hash = t2.hash
子查询向rownum
添加table2
列,其中包含从table1
中的最小ID开始的顺序ID。