用另一个表的数据更新一个表 [英] update one table with data from another

查看:34
本文介绍了用另一个表的数据更新一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表 1:

id    name    desc
-----------------------
1     a       abc
2     b       def
3     c       adf

表 2:

id    name    desc
-----------------------
1     x       123
2     y       345

我如何运行 sql update 查询,该查询可以使用相同的 ID 使用表 2 的名称和描述更新表 1?所以我得到的最终结果是

How do I run an sql update query that can update Table 1 with Table 2's name and desc using the same id? So the end result I would get is

表 1:

id    name    desc
-----------------------
1     x       123
2     y       345
3     c       adf

如何做到这一点:

  • SQL Server
  • MySQL
  • PostgreSQL
  • 甲骨文

推荐答案

对于 MySql:

UPDATE table1 JOIN table2 
    ON table1.id = table2.id
SET table1.name = table2.name,
    table1.`desc` = table2.`desc`

对于 SQL Server:

For Sql Server:

UPDATE   table1
SET table1.name = table2.name,
    table1.[desc] = table2.[desc]
FROM table1 JOIN table2 
   ON table1.id = table2.id

这篇关于用另一个表的数据更新一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆