Oracle將兩張表的資料插入第三張表且第三張表中不存在
阿新 • • 發佈:2019-07-21
1、由於是先查再插所以不能使用insert into table1() values(),
要使用insert into table1() select * table2,不能使用values。
2、因為是多張表故查詢和插入的時候需要指定是哪張表,否則會報錯:指定列模糊。
3、三張表聯查時可多次使用inner join等連線語法。
總結:在邏輯上SQL與Code實現基本一致,但是SQL的速度非常快,在具體實現上SQL需要非常強的邏輯性但是會省去很多寫code的功夫。
insert into SPC_RAW (DATA_INDEX,PARAMETER,VALUE,EQP,CHAMBER,UPDATE_TIME) select t2.RUNINDEX ,t2.IndicatorName ,t2.value,t1.EQPID,t1.UNITID, t1.DateTime from RUN_LIST t1 ,indicator_data t2 where not exists (select SPC_RAW.DATA_INDEX,SPC_RAW.PARAMETER,SPC_RAW.VALUE,SPC_RAW.EQP,SPC_RAW.CHAMBER,SPC_RAW.UPDATE_TIME from SPC_RAW inner join indicator_data on SPC_RAW.DATA_INDEX=indicator_data.RUNINDEX and SPC_RAW.PARAMETER=indicator_data.IndicatorName and SPC_RAW.VALUE=indicator_data.value inner join RUN_LIST on SPC_RAW.EQP=RUN_LIST.EQPID and SPC_RAW.CHAMBER=RUN_LIST.UNITID and SPC_RAW.UPDATE_TIME=RUN_LIST.DateTime );
&n