關於SQL 多表關聯update 解決示例

(一)問題產生數據庫

  1 問題是客戶要求數據庫中的一個字段須要進行批量的更新(從一張表裏拿一個字段的多個值,賦值給另外一張表的一個字段的多個值中)  --- 也不知道本身說明白沒!!post

   本身之前沒有寫過這樣的SQL ,因而在網上搜索了一下。找到了方法,寫了相關的SQL成功的解決了客戶的問題。很開心!!學習

 

(二)解決方案 spa

  1 多表關聯update單字段
    update stu t set t.NAME = (select t1.NAME from stu1 t1 where t1.ID = t.ID)
    where exists(select 1 from stu1 t2 where t2.ID = t.ID);code

  寫給客戶的SQL 以下 blog

update hi_psnjob t 
set t.jobglbdef5 = (
        select t1.glbdef2
        from om_post t1
        where t1.pk_post = t.pk_post and t.lastflag = 'Y'
)
where exists(select 1 from om_post t2 where t2.pk_post = t.pk_post and t.lastflag = 'Y')

  2多表關聯update多字段
    update stu t set (t.NAME, t.SEX) = (select t1.NAME, t1.SEX from stu1 t1 where t1.ID = t.ID)
    where exists(select 1 from stu1 t2 where t2.ID = t.ID);

ast

 

 


 

 

 

    知識不少,須要學習的地方還有不少。class

              慢慢來吧。date

相關文章
相關標籤/搜索