Oracle 联表更新(复杂查询及作用域)

规则

一条update只能修改一张表bai里的字段,但是可以du关联多张表去修改。

常用的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 两个表的关联字段)。

实例

1.由于业务规则的部分修改,需要添加字段,必须将旧数据表示该字段的值插入到更新表的新字段中

2.要关联表时,set field = value ,value 部分表示更新表 tb_case_manangement 的 tc(别名)作用域只能保持在第一个select查询中,无法存在嵌套子查询中

(1) ORA-00904: "TC"."art_id": 标识符无效;

update "tb_case_manangement" tc set tc."opu_time"=(
	select "operation_time" from (
		select "operation_time", ROWNUM num from (
			select ai."operation_time" from "tb_ai_record" ai where tc."art_id"=ai."art_id" and ai."operation_time" is not null order by "operation_time" desc
			) 
		) where num > 1
	);

(2)一定要现将查询结果先进行筛选,筛选后的结果可以根据更新表的字段确定唯一值时,就可以成功执行了;

(3)内部筛选的查询主要针对去重排序

	update "tb_case_manangement" tc set tc."opu_time"=(
		select "operation_time" from 
				(select t1."art_id", max(t1."operation_time") "operation_time" from "tb_ai_record" t1 LEFT JOIN "tb_ai_record" t2 on t1."art_id"=t2."art_id" and t1."operation_time"=t2."operation_time" where t1."operation_time" is not null and t1."art_id"=t2."art_id" group by t1."art_id") t
		where tc."art_id"=t."art_id"
	);

Guess you like

Origin blog.csdn.net/huofuman960209/article/details/109238373