我试图在我目前正在进行的项目中推广数据库版本控制系统(http://code.google.com/p/flyway/)的使用--为此,当我们对数据库进行更改时,我们将通过一系列的增量传播这些更改。目前还处于起步阶段,所以我们有机会制造“破坏性”三角洲并重新填充DB后三角洲。我们已经接近能够用生产数据对DB进行重构的时候了,所以我希望能够在理想的情况下仅在MySQL中将数据移动到数据库中。
例如,我想获取TABLE_1:the_value的列内容,在TABLE_2上创建列,然后将值从TABLE_1复制到id_org和id_club匹配的TABLE_2
TABLE 1
--------------------------------
| id_org | id_club | the_value |
--------------------------------
| 213 | 213 | 1 |
--------------------------------
| 214 | 213 | 2 |
--------------------------------
| 212 | 210 | 5 |
--------------------------------
-- intervening DDL that adds "the_value" to table 2
-- 2nd row on table 1 not copied across due to absence of matching id's
TABLE 2
--------------------------------------------
| id_org | id_club | foo | the_value |
--------------------------------------------
| 213 | 213 | bar | 1 |
--------------------------------------------
| 212 | 210 | bar | 5 |
--------------------------------------------我以前使用过带有触发器的FOR每一行构造,但在MySQL站点上找不到文档,这些文档建议或演示如何使用它迭代结果集,以便将其复制到目标表中。这可能更多地反映了我无法找到适当的信息。
有人能在这件事上给我指点吗?
发布于 2011-05-09 17:42:15
假设表2已经存在于id_org、id_club和foo中,只需创建'the_value‘列并使用联接发出更新调用
见更新手册
UPDATE table1 t1, table2 t2 SET t2.the_value=t1.the_value
WHERE t1.id_org = t2.id_org AND t1.id_club = t2.id_clubhttps://dba.stackexchange.com/questions/2603
复制相似问题