zoukankan      html  css  js  c++  java
  • sql多表关联更新—用b表字段来更新a表对应的字段

    在开发时,如果遇到表需要加字段,那么需要对存量数据刷新这个字段值

    1、mysql

    --两张表关联
    UPDATE JC_COLL_REPAY r
    INNER JOIN lc_lm_loan l ON r.loan_no = l.loan_no
    SET r.loan_typ = l.tep_cde,
      r.oper_center = l.bch_cde,
      r.chnl_cde = l.coopr_cde;
    
    --三张表关联
    UPDATE JC_COLL_REPAY r
    INNER JOIN lc_lm_loan l ON r.loan_no = l.loan_no
    INNER JOIN lc_cust_info i ON l.cust_id = i.cust_id
    SET r.loan_typ = l.tep_cde,
      r.oper_center = l.bch_cde,
      r.chnl_cde = l.coopr_cde,
      r.indiv_mobile = i.indiv_mobile;

    2、oracle

    ---三张表关联
    update JC_COLL_REPAY r
      set (r.indiv_mobile,r.loan_typ,r.oper_center,r.chnl_cde) =
      (select c.indiv_mobile,l.tep_cde,l.bch_cde,l.coopr_cde
        from lc_lm_loan l,lc_cust_info c
        where l.cust_id = c.cust_id and l.loan_no = r.loan_no
      )
      where exists (select 1
        from lc_lm_loan l,lc_cust_info c
        where l.cust_id = c.cust_id and l.loan_no = r.loan_no
      )
    
    
    ---不推荐
    update JC_COLL_REPAY r 
    set r.loan_typ=(select tep_cde from lc_lm_loan l where r.loan_no=l.loan_no),
      r.oper_center=(select bch_cde from lc_lm_loan l where r.loan_no=l.loan_no),
      r.chnl_cde=(select coopr_cde from lc_lm_loan l where r.loan_no=l.loan_no)
    where exists (select 1 from a where r.loan_no=l.loan_no)

    还可以使用merge into 

    merge into TEST_RESULT r
         using (select * from test_major m,test_subject s where m.id = s.sb_ma) ms
         on (r.subject = ms.sb_name)
         when matched then 
         update set r.major = ms.zymc
  • 相关阅读:
    对我影响最大的老师
    秋季学习总结
    介绍自己
    搭建新环境的准备工作
    我的技术博客开通啦!!
    java数组及数组的插入,删除,冒泡算法
    包(package)以及面向对象三个基本特征(继承)的介绍
    常用的Arrays类和二维数组以及二分法的介绍
    构造方法、封装、关键字(this、static)和代码块的介绍
    类和对象的介绍
  • 原文地址:https://www.cnblogs.com/pluto-yang/p/12531729.html
Copyright © 2011-2022 走看看