Thread: Update Join Query
Hi,
Looking for the correct syntax for an UPDATE LEFT JOIN query in PostgreSQL.
The equivalent MySQL query is :
UPDATE
Foo f LEFT JOIN Goo g on f.Foo_ID = g.Goo_ID
SET
f.Foo_ID = g.Goo_ID
WHERE
f.Foo_ID IS NOT NULL;
When I try to run this in Postgres, i get the following error:
ERROR: syntax error at or near "LEFT"
Is it possible to have UPDATE JOIN queries in PostgreSQL?
Thanks.
Looking for the correct syntax for an UPDATE LEFT JOIN query in PostgreSQL.
The equivalent MySQL query is :
UPDATE
Foo f LEFT JOIN Goo g on f.Foo_ID = g.Goo_ID
SET
f.Foo_ID = g.Goo_ID
WHERE
f.Foo_ID IS NOT NULL;
When I try to run this in Postgres, i get the following error:
ERROR: syntax error at or near "LEFT"
Is it possible to have UPDATE JOIN queries in PostgreSQL?
Thanks.
update foo set foo_id = g.goo_id from goo g where foo.foo_id = g.goo_id and foo.foo_id is not null I think. :) -Mark On Mon, 2008-06-23 at 21:43 +0200, Daniel Futerman wrote: > Hi, > > Looking for the correct syntax for an UPDATE LEFT JOIN query in > PostgreSQL. > > The equivalent MySQL query is : > > UPDATE > Foo f LEFT JOIN Goo g on f.Foo_ID = g.Goo_ID > SET > f.Foo_ID = g.Goo_ID > WHERE > f.Foo_ID IS NOT NULL; > > > When I try to run this in Postgres, i get the following error: > > ERROR: syntax error at or near "LEFT" > > Is it possible to have UPDATE JOIN queries in PostgreSQL? > > Thanks.
--- El lun 23-jun-08, Daniel Futerman <daniel.futerman@gmail.com> escribió: De: Daniel Futerman <daniel.futerman@gmail.com> |
--- El lun 23-jun-08, Daniel Futerman <daniel.futerman@gmail.com> escribió: De: Daniel Futerman <daniel.futerman@gmail.com> |
Daniel Futerman wrote: > Is it possible to have UPDATE JOIN queries in PostgreSQL? Yes: UPDATE target .... FROM othertable; As far as I know Pg can only do an inner join on the update target. This can be easily be turned into an outer join with something like: UPDATE target .... FROM target t LEFT OUTER JOIN othertable WHERE target.id = t.id; or similar. I haven't checked to see whether this results in an extra scan in the query plan; you might want to use EXPLAIN ANALYZE to examine how Pg will execute the query. -- Craig Ringer