英文:
Why am I getting an equijoin error when using INNER JOIN in redshift?
问题
Database is Redshift
I see this error:
[XX000] ERROR: Target table must be part of an equijoin predicate Where: SQL statement "update jwan.photos_audit_v4 set expiry_date = s.user_updated from jwan.photos_audit_v4 as t INNER JOIN _sc95897_tmp_photos_audit_v4 as s on t.user_id = s.user_id and t.position = s.position WHERE t.expiry_date is null AND s.idx = 1" PL/pgSQL function "sp_photos_v4_audit" line 272 at SQL statement
This is my code:
-- expire previous photos that have been replaced
update jwan.photos_audit_v4
set expiry_date = s.user_updated
from jwan.photos_audit_v4 as t
INNER JOIN _sc95897_tmp_photos_audit_v4 as s
on t.user_id = s.user_id and t.position = s.position
WHERE t.expiry_date is null AND s.idx = 1
;
I don't even conceptually understand what is happening. I'm not using an outerjoin so why is this happening?
From hevodata:
Table Update Using the Outer Joins in the FROM Clause
While performing an outer join to the target table with a FROM clause and an UPDATE statement, you will witness an error:
From AWS:
Updates with outer joins in the FROM clause
The previous example showed an inner join specified in the FROM clause of an UPDATE statement. The following example returns an error because the FROM clause does not support outer joins to the target table:
update category set catid=100
from event left join category cat on event.catid=cat.catid
where cat.catgroup='Concerts';
ERROR: Target table must be part of an equijoin predicate
If the outer join is required for the UPDATE statement, you can move the outer join syntax into a subquery:
update category set catid=100
from
(select event.catid from event left join category cat on event.catid=cat.catid) eventcat
where category.catid=eventcat.catid
and catgroup='Concerts';
What is going on in my code?
英文:
Database is Redshift
I see this error:
[XX000] ERROR: Target table must be part of an equijoin predicate Where: SQL statement "update jwan.photos_audit_v4 set expiry_date = s.user_updated from jwan.photos_audit_v4 as t INNER JOIN _sc95897_tmp_photos_audit_v4 as s on t.user_id = s.user_id and t.position = s.position WHERE t.expiry_date is null AND s.idx = 1" PL/pgSQL function "sp_photos_v4_audit" line 272 at SQL statement
This is my code:
-- expire previous photos that have been replaced
update jwan.photos_audit_v4
set expiry_date = s.user_updated
from jwan.photos_audit_v4 as t
INNER JOIN _sc95897_tmp_photos_audit_v4 as s
on t.user_id = s.user_id and t.position = s.position
WHERE t.expiry_date is null AND s.idx = 1
;
I don't even conceptually understand what is happening. I'm not using an outerjoin so why is this happening?
From hevodata:
> Table Update Using the Outer Joins in the FROM Clause
While performing an outer join to the target table with a FROM clause and an UPDATE statement, you will witness an error:
From AWS:
> Updates with outer joins in the FROM clause
The previous example showed an inner join specified in the FROM clause of an UPDATE statement. The following example returns an error because the FROM clause does not support outer joins to the target table:
update category set catid=100
from event left join category cat on event.catid=cat.catid
where cat.catgroup='Concerts';
ERROR: Target table must be part of an equijoin predicate
>If the outer join is required for the UPDATE statement, you can move the outer join syntax into a subquery:
update category set catid=100
from
(select event.catid from event left join category cat on event.catid=cat.catid) eventcat
where category.catid=eventcat.catid
and catgroup='Concerts';
What is going on in my code?
答案1
得分: 3
问题在于你的WHERE子句没有包括正在更新的表。我知道你可能认为已经包括了,但你只在FROM子句中引用了表jwan.photos_audit_v4。通常情况下,你不会在FROM子句中引用目标表,只会引用提供要更新数据的表。WHERE子句告诉Redshift要更新目标中的哪些行,但你没有指定这一点。因此,看起来你想要使用FROM子句中的所有值来更新每一行。
这是一个常见的错误。你的更新应该类似于:
update jwan.photos_audit_v4
set expiry_date = s.user_updated
from _sc95897_tmp_photos_audit_v4 as s
WHERE jwan.photos_audit_v4.expiry_date is null AND s.idx = 1
AND jwan.photos_audit_v4.user_id = s.user_id
AND jwan.photos_audit_v4.position = s.position;
英文:
The problem is that your WHERE clause doesn't include the table being updated. I know, you think it is, but you only reference the table jwan.photos_audit_v4 in the FROM clause. In general you don'r reference the target table in the FROM clause, only the tables needed to provide the data to be updated. The WHERE clause tells Redshift which rows in the target to update and you don't indicates this. So it looks like you want to update every row with all the values in the FROM clause.
This is a common error. Your update should look something like:
update jwan.photos_audit_v4
set expiry_date = s.user_updated
from _sc95897_tmp_photos_audit_v4 as s
WHERE jwan.photos_audit_v4.expiry_date is null AND s.idx = 1
AND jwan.photos_audit_v4.user_id = s.user_id
AND jwan.photos_audit_v4.position = s.position
;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论