sql - 从具有匹配值对的另一个表中插入
问题描述
Table_One Table_Two
ID ----- | ----- P_Name ID ----- | ----- P_Name
1X2 | Name1 | Name1
1X3 | Name2 | Name2
我想将 ID 插入 Table_Two 中 ID 与 Table_One 匹配的位置。例如 Table_2 上 Name2 的 ID 需要是 1X3
这是我到目前为止所拥有的,我遇到了错误。我对 SQL 很陌生。要温柔。
哦,我也在使用 PostgreSQL。
insert into table_two (ID)
select "ID" from Table_One
where Table_One.P_Name = Table_Two.P_name
解决方案
您想更新表,而不是insert。 Update
更改列中的值。 Insert
添加新行。所以:
update table_two t2
set id = t1.id
from table_one t1
where t1.name = t2.name;
推荐阅读
- mysql - 如何使用 MySQL 获得类似数据透视表的结果
- angular - 升级到 Angular 8.3.5 后,Angular 应用程序不加载应用程序模块/组件
- python - 如何将变量作为 url 的一部分?
- python-2.7 - 从 API 触发 ansible playbook 并在后台运行它们
- django - 来自 ListBox 的数据发布未呈现到数据库
- c# - 如何更新注释的内容
- angular - 反应式开发 | RxJS 异步管道不工作
- .net - .netcore 应用程序的重复主键数据库条目 mysql 错误
- docker - Docker 映像到期日期
- javascript - 在向 API 发出请求后,如何在从 Mobx 存储访问数据时修复页面的额外渲染?