需要sql insert查询

时间:2010-06-07 14:31:36

标签: sql ms-access

所以我有两张桌子。它们如下图所示。

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

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

总而言之,需要迁移用户表中不存在的来自主表的不同用户。只有它们共有的字段是收件人ID和收件人名称

This is the MASTER TABLE

this is the user table

2 个答案:

答案 0 :(得分: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

你需要对它进行修改,以便从那些来源的任何地方获取LastName,FirstName等。

答案 1 :(得分: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)