Select Table1 中的某列并使用 Where 子句条件插入到 Table2 中的某列

Select Certain Column from Table1 and Insert to Certain Column in Table2 with Where Clause Conditions

表 1:

|Id|Category_Id|Type_Id|Code|
+--+-----------+-------+----+
|1 |1          |1      |A   |
|3 |2          |1      |B   |
|4 |1          |3      |C   |

表 2:

|Id|Category_Id|Type_Id|Code_Id|
+--+-----------+-------+-------+    
|1 |1          |1      |NULL   |
|3 |2          |1      |NULL   |
|5 |9          |7      |NULL   |

如您所见,表 2 中的列 'Code_Id' 为 NULL。我需要使用表 1 中列 'Id' 中的值更新该列,条件是表 1 中列 'Category_Id' 和 'Type_Id' 中的值与列 'Category_Id' 和 [=28 中的值匹配=] 在表 2.

我该怎么做?谢谢并期待您的帮助。

使用加入更新

update t2 set code_id=t1.id
from table2
join table1 t1 on t1.type_id=t2.type_id and t1.category_id=t2.category_id

使用加入

UPDATE t2
SET Code_Id = t1.Id
FROM Table2 t2
JOIN Table1 t1 on t2.Type_Id=t1.Type_Id and t2.Category_Id=t1.Category_Id

它必须在所有条件下都需要别名,如集合,因为在多个表中可能是相同的列名而不是错误。

UPDATE A 
set A.code_id = B.id
FROM table2 A
INNER JOIN table1 B on B.type_id=A.type_id and B.category_id=A.category_id