postgresql - Postgresql:使用两个内部连接进行更新 [MySQL 到 PostgreSQL]
问题描述
在 MySQL 中,可以这样做:
update
table_a A
inner join
table_b B
on
A.field_five = B.field_five
inner join
table_c C
on
B.field_one = C.field_one and A.field_two = C.field_two
set A.field_three = C.field_four
我试图在 PostgreSQL 中构造相同的查询,如下所示:
update table_a A
set A.field_three = C.field_four
from table_b B
inner join table_c C
on
B.agency_id = C.agency_id and A.field_two = C.field_two
where
A.field_five = B.field_five
我收到以下错误:
错误:对表“a”的 FROM 子句条目的引用无效
我正在使用 PostgreSQL 11。在 postgres 中执行此查询的正确方法是什么?
解决方案
不要在“set”中指定要更新的表,并将“A.field_two = C.field_two”移动到 where 子句
update table_a A
set field_three = C.field_four
from table_b B
inner join table_c C
on
B.agency_id = C.agency_id
where
A.field_five = B.field_five
and A.field_two = C.field_two
推荐阅读
- javascript - Javascript:使用 try-catch 嵌套
- java - 制作一个可以保护我的教程视频不被复制的程序的步骤是什么?
- java - 如何在 MacOs 上使用 JDK 12 (sdkman) 配置 Apache NetBeans
- php - 对动态页面内容使用 Array Check 而不是 Switch 语句可以吗?
- ms-access - MS Access - 分组/子数据查询结果在表单中
- php - 如何在 WooCommerce /my-account/ 中编辑文本
- c# - 如何从列表中的字典中设置/检索值?
- batch-file - 批处理文件执行后退出 CMD
- fortran - Fortran 代码中与 EOF 命令相关的错误
- python - 尝试使用 python(matplotlib 和 numpy)绘制函数,如果我稍微改变它,就会遇到值错误和类型错误