SQL将丢失的记录插入到第3个表中,其他2个人拥有在第3个中配对的数据

时间:2014-08-14 20:57:16

标签: sql join insert multiple-tables

我有三个表格,在这里汇总到相关列(从原始帖子8/20/14修改):

  1. CODES(WAGECODE,DESCRIPTION),
  2. EMP(EMPCODE,FIRSTNAME,LASTNAME)
  3. EMPWAGE(EMPCODE,WAGECODE,DESCRIPTION,FIRSTNAME,LASTNAME)
  4. 有5个唯一的CODES.WAGECODE记录,每个记录都有描述。 有5个唯一的EMP.EMPCODE,包含名字和姓氏

    EMPWAGE表应该有5个EMP.EMPCODE(25条记录)中的每条记录

    我在SQLFiddle上的架构包含了16条所需的记录,还需要9条记录。 我有以下代码隔离了9条记录所需的数据:

    SELECT E.EMPCODE, C.WAGECODE, C.DESCRIPTION, E.FIRSTNAME, E.LASTNAME
    FROM
    (EMP E CROSS JOIN CODES C)
        LEFT JOIN EMPWAGE EW ON E.EMPCODE = EW.EMPCODE AND C.WAGECODE = EW.WAGECODE
    WHERE NOT EXISTS
          (SELECT 1
           FROM EMPWAGE EW
           WHERE
               E.EMPCODE = EW.EMPCODE AND C.WAGECODE = EW.WAGECODE
          )
    

    但是我坚持使用SQLFiddle插入此数据(9条记录)的过程: http://sqlfiddle.com/#!3/7cfe7/7,我将不胜感激。

    将以下内容添加到上面的第一行,我的SQLFiddle在运行

    后保持不变
    INSERT INTO EMPWAGE (EMPCODE, WAGECODE, DESCRIPTION, FIRSTNAME, LASTNAME)
    

    用于SQLFiddler http://sqlfiddle.com/#!3/7cfe7/7的架构:

    CREATE TABLE CODES (WAGECODE varchar(5) NOT NULL, DESCRIPTION varchar(30) NULL);
    
    Insert into CODES values ('001', 'RegHours');
    Insert into CODES values ('003', 'Holiday');
    Insert into CODES values ('008', 'Sick');
    Insert into CODES values ('010', 'PCH');
    Insert into CODES values ('397', 'Comp');
    
    CREATE TABLE EMP (EMPCODE varchar(11) NOT NULL, FIRSTNAME varchar(15) NULL,
    LASTNAME varchar(15) NULL);
    
    Insert into EMP values ('BretV', 'Bret', 'Vick');
    Insert into EMP values ('CarlS', 'Carl', 'Shaw');
    Insert into EMP values ('MaryC', 'Mary', 'Comb');
    Insert into EMP values ('RickK', 'Rick', 'Kite');
    Insert into EMP values ('TamiB', 'Tami', 'Ball');
    
    
    CREATE TABLE EMPWAGE (EMPCODE varchar(11) NOT NULL, WAGECODE varchar(7) NOT NULL,
    DESCRIPTION varchar(30) NULL, FIRSTNAME varchar(15) NULL, LASTNAME varchar(15) NULL);
    
    Insert into EMPWAGE values ('BretV', '001', 'RegHours', 'Bret', 'Vick');
    Insert into EMPWAGE values ('BretV', '003', 'Holiday',  'Bret', 'Vick');
    Insert into EMPWAGE values ('BretV', '008', 'Sick',     'Bret', 'Vick');
    Insert into EMPWAGE values ('BretV', '010', 'PCH',      'Bret', 'Vick');
    Insert into EMPWAGE values ('BretV', '397', 'Comp',     'Bret', 'Vick');
    Insert into EMPWAGE values ('CarlS', '001', 'RegHours', 'Carl', 'Shaw');
    Insert into EMPWAGE values ('CarlS', '010', 'PCH',      'Carl', 'Shaw');
    Insert into EMPWAGE values ('MaryC', '001', 'RegHours', 'Mary', 'Comb');
    Insert into EMPWAGE values ('MaryC', '003', 'Holiday',  'Mary', 'Comb');
    Insert into EMPWAGE values ('MaryC', '397', 'Comp',     'Mary', 'Comb');
    Insert into EMPWAGE values ('RickK', '001', 'RegHours', 'Rick', 'Kite');
    Insert into EMPWAGE values ('RickK', '008', 'Sick',     'Rick', 'Kite');
    Insert into EMPWAGE values ('TamiB', '001', 'RegHours', 'Tami', 'Ball');
    Insert into EMPWAGE values ('TamiB', '003', 'Holiday',  'Tami', 'Ball');
    Insert into EMPWAGE values ('TamiB', '008', 'Sick',     'Tami', 'Ball');
    Insert into EMPWAGE values ('TamiB', '010', 'PCH',      'Tami', 'Ball');
    

0 个答案:

没有答案