gpt4 book ai didi

sql - 从 Table1 中选择某列并插入到 Table2 中的某列,并使用 Where 子句条件

转载 作者:行者123 更新时间:2023-12-01 11:14:11 25 4
gpt4 key购买 nike

表 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”列中的值与表 2 中“Category_Id”列和“Type_Id”列中的值匹配。

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

最佳答案

使用加入更新

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

关于sql - 从 Table1 中选择某列并插入到 Table2 中的某列,并使用 Where 子句条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55176662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com