记录应该从表 A 插入到表 B,条件是表 A 中具有 RoleId=1 的所有 Id 都应该使用 SQL 查询在表 B 中可用
表A
Id RoleId
1 1
2 1
3 1
4 2
5 2
表 B
Id
1
输出:表 B
Id
1
2
3
我尝试了以下查询但无法写入条件
insert into Table B (Id) select Id from Table A where roleId=1
答案 0 :(得分:1)
这是您需要做的:
insert into tableB (Id)
select Id from TableA
where RoleId = 1
and not exists (select 1 from TableB where TableA.id = TableB.ID)