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

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

问题描述

表1:

  ID名称desc 
------------- ----------
1 a abc
2 b def
3 c adf

表2:

  ID名称desc 
------ -----------------
1 x 123
2 y 345

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



表1:

  id name desc 
-----------------------
1 x 123
2 y 345
3 c如何做到这一点:



  • SQL Server

  • MySQL

  • PostgreSQL

  • Oracle


  • 解决方案

    对于MySql:

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

    对于Sql Server:

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


    Table 1:

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

    Table 2:

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

    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

    Table 1:

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

    How can this be done for:

    • SQL Server
    • MySQL
    • PostgreSQL
    • Oracle

    解决方案

    For MySql:

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

    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天全站免登陆