Re: [GENERAL] 3 tables join update

2007-10-01 Thread Alban Hertroys
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
[EMAIL PROTECTED]

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 //

---(end of broadcast)---
TIP 5: don't forget to increase your free space map settings


Re: [GENERAL] 3 tables join update

2007-09-30 Thread rihad

Richard Broersma Jr wrote:

--- rihad <[EMAIL PROTECTED]> 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).


---(end of broadcast)---
TIP 9: In versions below 8.0, the planner will ignore your desire to
  choose an index scan if your joining column's datatypes do not
  match


Re: [GENERAL] 3 tables join update

2007-09-30 Thread rihad

Richard Broersma Jr wrote:

--- rihad <[EMAIL PROTECTED]> 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.

---(end of broadcast)---
TIP 9: In versions below 8.0, the planner will ignore your desire to
  choose an index scan if your joining column's datatypes do not
  match


Re: [GENERAL] 3 tables join update

2007-09-29 Thread Richard Broersma Jr
--- rihad <[EMAIL PROTECTED]> 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.



---(end of broadcast)---
TIP 9: In versions below 8.0, the planner will ignore your desire to
   choose an index scan if your joining column's datatypes do not
   match


[GENERAL] 3 tables join update

2007-09-29 Thread rihad

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.

---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

  http://www.postgresql.org/docs/faq