溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

merge into基本用法

發(fā)布時間:2020-08-04 15:47:15 來源:ITPUB博客 閱讀:269 作者:llnnmc 欄目:關系型數(shù)據(jù)庫

由于merge into平時很少用,但這次用到它來給記錄做插入更新,于是簡單記下最基本的用法。這里的例子就是給一個表中符合條件的數(shù)據(jù)做個值計數(shù)的更新,如果找到符合ID條件的記錄,那么就將其值字段加1,否則就插入這條新的記錄,并初始化值。

創(chuàng)建測試表并插入數(shù)據(jù):
create table test1(id number, val number);
insert into test1 values(101, 1);
insert into test1 values(102, 1);
commit;
select * from test1;

        ID        VAL
---------- ----------
       101          1
       102          1

做merge into操作,新的一條數(shù)據(jù)被插入:
merge into test1 t1
using (select count(*) cnt from test1 where id = 103) t2 on (cnt <> 0)
when matched then
  update set val = val + 1 where id = 103
when not matched then
  insert values(103, 1);
commit;
select * from test1;

        ID        VAL
---------- ----------
       101          1
       102          1
       103          1

再執(zhí)行一個merge into后,數(shù)據(jù)被更新:

        ID        VAL
---------- ----------
       101          1
       102          1
       103          2


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI