oracle update left join查詢


對於有的更新語句,要更新的表可能條件不夠,需要用到left join關聯其他表,

但是不能直接關聯,否則報錯:錯誤如下:

update  imim_gireqbillitems gi 
left join imim_gireqbills g on g.guid=gi.guid
set gi.quantity=
(
    select sum(gi2.quantity)
    from imim_gireqbillitems gi2
    left join imim_gireqbills g2 on g2.guid=gi2.guid
    where g2.ismerge='0' and g2.mergeid=g.mergeid
)
where g.ismerge='1' and exists
(
    select 1
    from imim_gireqbillitems gi2
    left join imim_gireqbills g2 on g2.guid=gi2.guid
    where g2.ismerge='0' and g2.mergeid=g.mergeid
)

正確實例:

update 
(
  select * from imim_gireqbillitems gi 
  left join imim_gireqbills g on g.guid=gi.guid
  where g.ismerge='1'
) T
set T.quantity=
(
    select sum(gi2.quantity)
    from imim_gireqbillitems gi2
    left join imim_gireqbills g2 on g2.guid=gi2.guid
    where g2.ismerge='0' and g2.mergeid=T.mergeid
)
where exists
(
    select 1
    from imim_gireqbillitems gi2
    left join imim_gireqbills g2 on g2.guid=gi2.guid
    where g2.ismerge='0' and g2.mergeid=T.mergeid
)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM