由于您table2
没有candidateid,我假设在插入时,您确定其中包含的所有技能都table2
属于candidateid = 1。
如果假设正确,那么您可以按照下面的示例将新技能从 中table2
插入table1
。
编辑:用评论更新了查询。
use tempdb
-- Pretending this is your table1
create table candidateComputerSkillMapping
(
candidateid int not null
, skillname varchar(128) not null
, unique
(
candidateid
, skillname
)
)
-- Table1 has one skill for candidateid = 1
insert candidateComputerSkillMapping values (1, 'Old Skill')
-- Prentending this is your table2
create table tempComputerSkillMap
(
skillname varchar(128) not null
)
-- table2 has two skills. Now since this table doesn't have candidateid, how do I know the skills are for candidateid = 1?
-- I don't know and I am assuming that you are certain that these skills indeed belong to candidateid = 1.
insert tempComputerSkillMap values ('Old skill')
insert tempComputerSkillMap values ('New skill')
-- Insert only new skills from table2 into table1 for candidateid = 1
insert candidateComputerSkillMapping (candidateid, skillname)
select 1, t2.skillname
from tempComputerSkillMap t2
where not exists (select * from candidateComputerSkillMapping t1 where t1.candidateid = 1 and t1.skillname = t2.skillname)
select * from candidateComputerSkillMapping