[英]Insert record from one table from another table base on condition
記錄應該從表 A 插入到表 B 中,條件是表 A 的所有具有 RoleId = 1 的 Id 都應該在表 B 中使用 SQL 查詢
表 A
Id RoleId
1 1
2 1
3 1
4 2
5 2
表 B
Id
1
Output:表 B
Id
1
2
3
我嘗試了以下查詢但無法編寫條件
insert into Table B (Id) select Id from Table A where roleId=1
這是您需要做的:
insert into tableB (Id)
select Id from TableA
where RoleId = 1
and not exists (select 1 from TableB where TableA.id = TableB.ID)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.