将一个表中的某个字段根据id关联写入到另一张表中的某个字段中去

时间:2022-12-05 15:08:45


分析:tb_ac_acctinfo中的acctname目前是没有数据的

           tb_ac_custinfo中的custname是有数据的

            两张表中都有custid字段

 需求:将tb_ac_custinfo和tb_ac_acctinfo 根据custid联系起来,将tb_ac_custinfo中的custname写入到tb_ac_acctinfo表中的acctname中去。

sql语句如下:

update tb_ac_acctinfo a 
set a.acctname = (select b.custname from tb_ac_custinfo b where a.custid=b.custid)
where a.custid in (select custid from tb_ac_custinfo)

这边需要确保查出来的数据返回只有一个值哦,不然会报返回值不值一个的错误哦。