从表a中的表b中插入b中不存在的表

时间:2014-01-17 17:12:19

标签: mysql

为什么这似乎不适用于MySQL?当我运行select时,它返回记录,但记录在两个表中。它应该只返回stg中不在vista_hospital_hap3中的记录。我想从stg表插入到vista_hospital_hap3,它不在vista_hospital_hap3中。

所以基本上尝试插入新记录。

INSERT INTO VISTA_HOSPITAL_HAP3
 ( DESCRIPTION,
   LOCATION_PLACE,
   LOCATION_AREA,
   LOCATION_CD,
   SOURCE_ADMISSION_ID,
   SOURCE_SYSTEM_NAME,
   SOURCE_TABLE_NAME,
   SOURCE_TABLE_KEY,
   TS,
   VALUE,
   VALUE_TEXT,
   VALUE_DATE,
   EXTRACT_DATE,
   LOAD_DATE,
   SOURCE_PATIENT_ID,
   HASH_KEY,
   RGI_LOAD_DATE,
   RGI_LAST_UPDATE_DATE,
   SUBIEN)
 SELECT 
   stg.DESCRIPTION,
   stg.LOCATION_PLACE,
   stg.LOCATION_AREA,
   stg.LOCATION_CD,
   stg.SOURCE_ADMISSION_ID,
   stg.SOURCE_SYSTEM_NAME,
   stg.SOURCE_TABLE_NAME,
   stg.SOURCE_TABLE_KEY,
   stg.TS,
   stg.VALUE,
   stg.VALUE_TEXT,
   stg.VALUE_DATE,
   stg.EXTRACT_DATE,
   stg.LOAD_DATE,
   stg.SOURCE_PATIENT_ID,
   stg.HASH_KEY,
   stg.RGI_LOAD_DATE,
   stg.RGI_LAST_UPDATE_DATE,
   stg.SUBIEN

  FROM STG_VISTA_HOSPITAL_HAP3 stg  LEFT OUTER JOIN VISTA_HOSPITAL_HAP3 trg 
                                    ON  stg.DESCRIPTION = trg.DESCRIPTION         
                          AND stg.SOURCE_ADMISSION_ID = trg.SOURCE_ADMISSION_ID
                          AND stg.SOURCE_TABLE_NAME   = trg.SOURCE_TABLE_NAME
                          AND stg.SUBIEN              = trg.SUBIEN 
 WHERE 
       trg.DESCRIPTION         IS NULL
 AND   trg.SOURCE_ADMISSION_ID IS NULL
 AND   trg.SOURCE_TABLE_NAME   IS NULL
 AND   trg.SUBIEN              IS NULL;

0 个答案:

没有答案