开发者

SQL update多表关联更新方法解读

开发者 https://www.devze.com 2024-08-13 11:48 出处:网络 作者: 猫饭_ACE
目录1. mysql2. oracle多表关联,更新全表中的某一字段多表关联,更新指定行的某一字段3. SQLServer4. 通用方法总结1. MySQL
目录
  • 1. mysql
  • 2. oracle
    • 多表关联,更新全表中的某一字段
    • 多表关联,更新指定行的某一字段
  • 3. SQLServer
    • 4. 通用方法
      • 总结

        1. MySQL

        update test1,test2 
        set test1.name=test2.name,test1.age=test2.age
        where test1.id=test2.id

        2. oracle

        多表关联,更新全表中的某一字段

        • EX1:
        UPDATE REjavascriptDREPORT T  SET T.SSHY=(SELECT  D.SSHY FROM DATA_COMPANY D WHEhULoXORE T.dwDM =D.DWDM 
        and rownum=1 );
        • EX2:
        update test1 
        set (test1.name,test1.age)=
        (select test2.name,test2.age from test2 where test2.id=test1.id)

        多表关联,更新指定行的某一字段

        UPDATE "DATA_HISTORY_copy2" t 
        set SHJG = (select m.SHJG from DATA_MONTHCOPY m where t.DWDM = m.DWDM and SUBSTR(t.SQSJ, 编程0, 8) = SUBSTR(m.SQShttp://www.devze.comJ, 0, 8)) 
        WHERE EXISTS (select t.* from "DATA_HISTORY_copy2" t,DATA_MONTHCOPY m where t.DWDM = m.DWDM and SUBSTR(t.SQSJ, 0, 8) = SUBSTR(m.编程SQSJ, 0, 8))

        3. SQLServer

        update test1
        set test1.name=test2.name,test1.age=test2.age
        from test1 
        inner join test2
        on test1.id=test2.id

        4. 通用方法

        update test1 
        set name=(select name from test2 where test2.id=test1.id),
        age=(select age from test2 where test2.id=test1.id)

        总结

        以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

        0

        精彩评论

        暂无评论...
        验证码 换一张
        取 消

        关注公众号