update后是否可以接两张表?

如题所述

一条update只能修改一张表里的字段,但是可以关联多张表去修改。不知道你用的是什么数据库。
常用的sqlserver格式如下:


update table1 set a.字段1=b.字段1,....,a.字段N=b.字段N from table1 a,table2 b where 两个表的关联字段。


常用的oracle格式如下:


update table1 a set (a.字段1,....,a.字段N) =(select b.字段1,...,b.字段N from table2 b where 两个表的关联字段) where exists (select 1 from table2 b where 两个表的关联字段)。


注意oracle语句里的exists不能省略,否则会导致没有对应关系的数据修改错误,甚至会报错。

EXISTS 指定一个子查询,检测 行 的存在。 

检测select 1 from yls_test c where a.cert_no =c.cert_no 是否存在如果存在执行update 如果不存在,语句就成了 update month_apply a set a.emp_name = (select name from yls_test b where a.cert_no = b.cert_no) 这样的语句是不执行更新命令的。

温馨提示:答案为网友推荐,仅供参考
相似回答