当前位置: 代码网 > it编程>数据库>MsSqlserver > SQL update多表关联更新方法解读

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

2024年08月02日 MsSqlserver 我要评论
1. mysqlupdate test1,test2 set test1.name=test2.name,test1.age=test2.agewhere test1.id=test2.id2. or

1. mysql

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

2. oracle

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

  • ex1:
update redreport t  set t.sshy=(select  d.sshy from data_company d where 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.sqsj, 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)

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com