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

5 篇文章 0 订阅
本文介绍了SQL中update语句的使用规则,强调一条update语句只能修改一张表中的字段,但可以关联多表进行操作。通过示例展示了在SQL Server和Oracle中的不同写法,并提供了一个具体的业务场景:如何根据旧数据添加新字段。同时,解释了在关联表时设置字段值的限制和解决去重排序问题的方法。
摘要由CSDN通过智能技术生成

规则

一条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"
	);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

day day day ...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值