xuhanchn 2016-05-17 03:41 采纳率: 100%
浏览 2081
已采纳

求解这个ORACLE三表联合更新的语句怎么写

t1

id  name

t2

id code 

t3

id code fullname time

条件:t1.id = t2.id , t2.code = t3.code;
且t3的code的值是可重复的,需要根据time取最新值;
目的:把t3.fullname 的值赋给 t1.name

我自己写了个语句,但是不知道哪里错了?

update (select t1.name, t3.fullname
from t1, t2, t3
where t1.id = t2.id and t2.code = t3.code
and not exists(select 1 from t3 b where b.code = t3.code and b.time > t3.time))
set t1.name = t3.fullname
  • 写回答

2条回答 默认 最新

  • xuhanchn 2016-05-19 01:34
    关注

    试了很多方法都不行,总会出一些问题,一般是因为关联的条件不是unique index/primary key,或者语法本身就有问题。
    (我提问中那个方法就是由于关联条件不是唯一键值才出错的)

    最后我自己搞出了这个方法,供有相同问题的同学参考

         update t1 A set A.name=
        (select C.fullname 
                from t2 B,  (select * from t3 x 
                        where x.time is not null 
                        and not exists where (select 1 from t3 y where y.code = x.code and y.time > x.time
                        ) C
                where B.id = A.id and C.code = B.code
        )
        where exists
        (select 1 
                from t2 B,  (select * from t3 x 
                        where x.time is not null 
                        and not exists where (select 1 from t3 y where y.code = x.code and y.time > x.time
                        ) C
                where B.id = A.id and C.code = B.code
        )
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog