update关联其他表批量更新数据
??? 為了做分析,需要整合一些數據到一個表中,涉及到通過主鍵關聯多個表,獲取其中的某些字段的值,通過update可以簡單實現,找了一篇文章,挺不錯:
??? 批量更新表時,update一個表的列時,需要依賴另外的表,這種依賴可以是where條件子句,也可以要update的field的值依賴另外的表
?? 通常有兩種做法
?? 1.使用存儲過程
?? 2.在程序代碼里逐條循環執行
這里給出一種更高效、簡潔的做法,批量更新SQL ,一句SQL就可以替代麻煩的循環過程,有MS SQLServer、Oracle、DB2下的寫法
--關鍵點:t4和t1是同一個table,primary key肯定也是同一個,
--并以它進行關聯,這樣在 select語句里即可引用到要update的表的fields
UPDATE Table1 AS t1
SET (Field1,Field2) = (SELECT Field21, Field22
?????????????????????? FROM Table2 t2
??????????????????????????? INNER JOIN Table3 t3
???????????????????????????????? ON t3.Field31 = t2.Field23
??????????????????????????? INNER JOIN Table4 t4
???????????????????????????????? ON t3.Field32 = t4.Filed41
?????????????????????? WHERE t2.Field24 >= ''
????????????????????????????????????? AND t1.fId = t4.fId);
----------------------------MS SQLServer --------------------------------------
UPDATE t1
SET Field1 = Field21, Field2 = Field22
FROM Table2 t2
???????? INNER JOIN Table3 t3
????????????? ON t3.Field31 = t2.Field23
???????? INNER JOIN Table4 t4
????????????? ON t3.Field32 = t4.Filed41
WHERE ((t2.Field24 >= '')
????????????????? AND t1.fId = t4.fId);
----------------------------Oracle --------------------------------------------
UPDATE Table1 t1
SET (Field1,Field2) = (SELECT Field21, Field22
?????????????????????? FROM Table2 t2
??????????????????????????? INNER JOIN Table3 t3
???????????????????????????????? ON t3.Field31 = t2.Field23
??????????????????????????? INNER JOIN Table4 t4
???????????????????????????????? ON t3.Field32 = t4.Filed41
?????????????????????? WHERE ((t2.Field24 >= '')
???????????????????????????????? AND t1.fId = t4.fId))
WHERE EXISTS (SELECT Field21, Field22
????????????? FROM Table2 t2
?????????????????? INNER JOIN Table3 t3
??????????????????????? ON t3.Field31 = t2.Field23
?????????????????? INNER JOIN Table4 t4
??????????????????????? ON t3.Field32 = t4.Filed41
????????????? WHERE ((t2.Field24 >= '')
?????????????????????????????? AND t1.fId = t4.fId));
---------------------------------DB2 ------------------------------------------
UPDATE Table1 AS t1
SET (Field1,Field2) = (SELECT Field21, Field22
?????????????????????? FROM Table2 t2
??????????????????????????? INNER JOIN Table3 t3
???????????????????????????????? ON t3.Field31 = t2.Field23
??????????????????????????? INNER JOIN Table4 t4
???????????????????????????????? ON t3.Field32 = t4.Filed41
?????????????????????? WHERE ((t2.Field24 >= '')
???????????????????????????????????????? AND t1.fId = t4.fId))
WHERE EXISTS (SELECT Field21, Field22
????????????? FROM Table2 t2
?????????????????? INNER JOIN Table3 t3
??????????????????????? ON t3.Field31 = t2.Field23
?????????????????? INNER JOIN Table4 t4
??????????????????????? ON t3.Field32 = t4.Filed41
????????????? WHERE ((t2.Field24 >= '')
??????????????????????????????? AND t1.fId = t4.fId));
?
轉載于:https://www.cnblogs.com/badwood316/archive/2011/06/12/2079242.html
總結
以上是生活随笔為你收集整理的update关联其他表批量更新数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言与或非逻辑符号_c语言逻辑与或非
- 下一篇: 异或版swap注意