2010-06-07 80 views
-1

所以我有两个表。它们如下图所示。SQL插入查询需要

我有一个主表“all_reports”。和一个用户表“用户列表”。主表可能包含用户列表中不存在的用户。我需要将它们添加到用户列表中。

主表中可能有重复项(检查图片)。主列表不包含用户列表需要的所有信息(没有管理员,没有HR状态,没有部门......再次检查图片)。

综上所述,主表中不存在于用户表中的不同用户需要迁移。只有那些共同对他们领域是接收者ID,以及收件人姓名

This is the MASTER TABLE

this is the user table

+0

你可以发布更大的图片,这很难猜测列名,谢谢 – kamahl 2010-06-07 14:37:16

+0

你到目前为止尝试了什么?你自己做这些事情的问题是什么? – Paul 2010-06-07 14:37:39

+0

我想你忘了一些东西。可能是问题。 – Salil 2010-06-07 14:37:48

回答

1
INSERT INTO User_List 
(RecipientId) 
SELECT DISTINCT recpid 
FROM All_Reports ar 
LEFT OUTER JOIN User_List ul 
ON ul.RecipientId = ar.recpid 
WHERE ul.RecipientId IS NULL 

你需要对其进行修改,以获得姓氏,名字等,从无论这些人的来源在哪里。

0
Insert into user_table(recipient ID, recipient_name) 
SELECT recipient ID, recipient_name from master_table as m 
where not eixts(select * from user_table 
where recipient ID=m.recipient ID and recipient_name=m.recipient name)