mysql - 更新表之间的行

时间:2011-01-31 21:19:05

标签: mysql sql-update

我有两张结构相同的桌子。

old_table看起来像这样的东西(例如,不是实际的表):

Name  -  DOB  - id
John  -  xxxx - 344

new_table看起来像这样:

Name  - DOB  - id
John  - 1980 - 344

其中new_table填充了DOB列.ID字段(唯一)和结构的其余部分在表之间是相同的。我想用new_table中的值更新old_table中的DOB字段,其中ID字段是相同的(在上面的示例中,'id'= 344,等等,对于所有行和ID)。

我在考虑使用: INSERT INTO old_table(DOB)SELECT DOB from new_table WHERE ...

然后我的mysql知识落空了。我应该使用INSERT还是可以在这里使用UPDATE?我如何只从old_table中提取DOB值,其中ID字段= new_table的ID字段?

谢谢..

2 个答案:

答案 0 :(得分:4)

UPDATE old_table, new_table
    SET old_table.DOB = new_table.DOB
    WHERE old_table.id = new_table.id

编辑:根据OP的评论

UPDATE old_table, new_table
    SET old_table.DOB = new_table.DOB
    WHERE old_table.id = new_table.id
        AND old_table.DOB = 'xxxx'
        AND old_table.field4 = '-'

答案 1 :(得分:0)

使用更新 更新old table ot set DOB =(从newTable nt中选择DOB,其中nt.id = ot.id) 类似的东西应该起作用

相关问题