2

我可以使用 SQL 将数据提取到新表中,两个现有表中的任何重复项都可以吗?

下表是我访问前从 ERP 系统拍摄的快照。

PRE
CO          Otp Los His Whs Fac
11889204    C20 22  66  1BA 1BA
11889132    C23 22  66  1BA 1BA
11888835    C20 23  66  1BA 1BA
11887689    C20 22  66  1BA 1BA
11884977    C20 22  22  1BA 1BA
11884624    C20 22  99  1BA 1BA

下表是我访问后拍摄的快照。

POST
CO          Otp Los His Whs Fac
11889204    C20 22  66  1BA 1BA
11889132    C23 22  66  1BA 1BA
11888835    C20 23  66  1BA 1BA
11882804    C20 22  77  1BA 1BA
11882724    C20 22  77  1BA 1BA
11882521    C20 22  77  1BA 1BA
11882431    C20 22  22  1BA 1BA
11881410    C20 22  22  1BA 1BA
11881282    C20 22  22  1BA 1BA

现在我想要的是 POST 访问表只显示新记录,以及 SQL 删除在 PRE 和 POST 表中重复的任何记录并创建一个新表并将重复项粘贴到那里。

现在我正在学习 SQL,但这有点超出我的深度。任何人都知道这是否可能?

4

1 回答 1

2

这会将 PRE 和 POST 表中的匹配行插入newtable

INSERT INTO newtable (POSTCO,Otp, Los, His, Whs, Fac) 
SELECT POSTCO,Otp, Los, His, Whs, Fac FROM posttable
WHERE EXISTS 
(SELECT POSTCO,Otp, Los, His, Whs, Fac FROM pretable
    WHERE POSTCO = posttable.POSTCO AND Otp = posttable.Opt AND 
    Los = posttable.Los AND His = posttable.His AND 
    Whs = posttable.Whs AND Fac = posttable.Fac)

然后删除重复的行:

DELETE FROM posttable WHERE EXISTS
(SELECT POSTCO,Otp, Los, His, Whs, Fac 
    FROM pretable
    WHERE POSTCO = posttable.POSTCO AND Otp = posttable.Opt 
    AND Los = posttable.Los AND His = posttable.His 
    AND Whs = posttable.Whs AND Fac = posttable.Fac)
于 2013-10-09T10:50:32.237 回答