3 tables join update

Started by rihadover 18 years ago5 messagesgeneral
Jump to latest
#1rihad
rihad@mail.ru

Hi,

Say I want to update table Foo with data in table Bar iff left join
between Foo and yet another table Baz finds no match.

UPDATE Foo foo LEFT JOIN Bar bar ON (foo.bar_id=bar.id)
SET foo.baz_id=baz.id
FROM Baz baz
WHERE bar.id IS NULL;

ERROR: syntax error at or near "LEFT"

UPDATE Foo foo
SET foo.baz_id=baz.id
FROM Baz baz LEFT JOIN Bar bar ON (foo.bar_id=bar.id)
WHERE bar.id IS NULL;

ERROR: invalid reference to FROM-clause entry for table "foo"
HINT: There is an entry for table "foo", but it cannot be referenced
from this part of the query.

Is it possible to rewrite this so that it does what I want in a single
query? Important: performance matters.

Thanks.

#2Richard Broersma Jr
rabroersma@yahoo.com
In reply to: rihad (#1)
Re: 3 tables join update
--- rihad <rihad@mail.ru> wrote:

UPDATE Foo foo
SET foo.baz_id=baz.id
FROM Baz baz LEFT JOIN Bar bar ON (foo.bar_id=bar.id)
WHERE bar.id IS NULL;

This query cannot work. Basiclly, you are trying to set the foo.baz_id = baz.id for records in
foo that do not yet exist. Doing this is impossible.

For this to work, you should be using an INSERT query to _ADD_ records to foo that do not yet
exist.

Regards,
Richard Broersma Jr.

#3rihad
rihad@mail.ru
In reply to: Richard Broersma Jr (#2)
Re: 3 tables join update

Richard Broersma Jr wrote:

--- rihad <rihad@mail.ru> wrote:

UPDATE Foo foo
SET foo.baz_id=baz.id
FROM Baz baz LEFT JOIN Bar bar ON (foo.bar_id=bar.id)
WHERE bar.id IS NULL;

This query cannot work.

I know. So how do I do it efficiently?

Thanks.

#4rihad
rihad@mail.ru
In reply to: Richard Broersma Jr (#2)
Re: 3 tables join update

Richard Broersma Jr wrote:

--- rihad <rihad@mail.ru> wrote:

UPDATE Foo foo
SET foo.baz_id=baz.id
FROM Baz baz LEFT JOIN Bar bar ON (foo.bar_id=bar.id)
WHERE bar.id IS NULL;

This query cannot work. Basiclly, you are trying to set the foo.baz_id = baz.id for records in
foo that do not yet exist. Doing this is impossible.

Sorry the query failed victim of me trying to simplify it so I forgot
the foo=bar join. Here's a 2 table join suffering from the same problem:
I want the update only when bar.common_field IS NULL:

UPDATE Foo foo
SET ...
FROM LEFT JOIN Bar bar USING(common_field)
WHERE blah='blah' AND bar.common_field IS NULL;

ERROR: syntax error at or near "JOIN"

I know I'm misusing UPDATE ... FROM because I don't really want Bar's
values to go into Foo, but only using them for a conditional update
(atomically I hope).

#5Alban Hertroys
a.hertroys@magproductions.nl
In reply to: rihad (#4)
Re: 3 tables join update

rihad wrote:

Richard Broersma Jr wrote:
UPDATE Foo foo
SET ...
FROM LEFT JOIN Bar bar USING(common_field)
WHERE blah='blah' AND bar.common_field IS NULL;

ERROR: syntax error at or near "JOIN"

I know I'm misusing UPDATE ... FROM because I don't really want Bar's
values to go into Foo, but only using them for a conditional update
(atomically I hope).

Oh, you mean:

UPDATE foo
SET ...
WHERE blah = 'blah'
AND NOT EXISTS (
SELECT 1 FROM baz WHERE foo.common_field = baz.common_field
)

--
Alban Hertroys
alban@magproductions.nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
7500 AK Enschede

// Integrate Your World //