pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedupdate on join ?
4+ messages / 4 participants
[nested] [flat]
* update on join ?
@ 2007-11-22 02:58 Andreas <maps.on@gmx.net>
2007-11-22 03:34 ` Re: update on join ? Frank Bax <fbax@sympatico.ca>
2007-11-22 03:38 ` Re: update on join ? Richard Broersma Jr <rabroersma@yahoo.com>
2007-11-22 03:41 ` Re: update on join ? chester c young <chestercyoung@yahoo.com>
0 siblings, 3 replies; 4+ messages in thread
From: Andreas @ 2007-11-22 02:58 UTC (permalink / raw)
To: pgsql-sql
Hi,
I'd like to update a table efficiently where the relevant select-info
is in another table that is foreign-linked.
Stupid example. 2 tables:
things (thing_id integer, name varchar(100), color varchar(100))
inventory (item_id integer, thing_fk integer references things
(thing_id), number)
For some reason I'd want to set the number of every red item to 0.
This inventory doesn't contain the color but the foreign key to the
other table where the color is found.
I tried
UPDATE things JOIN inventory ON things.thing_id = inventory.thing_fk
SET number = 0
WHERE color = 'red'
PSQL didn't like the JOIN though.
It works like this:
UPDATE inventory
SET number = 0
WHERE thing_fk IN (SELECT thing_id FROM things WHERE color = 'red')
It's effective but is it efficient, too?
Regards
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: update on join ?
2007-11-22 02:58 update on join ? Andreas <maps.on@gmx.net>
@ 2007-11-22 03:34 ` Frank Bax <fbax@sympatico.ca>
2 siblings, 0 replies; 4+ messages in thread
From: Frank Bax @ 2007-11-22 03:34 UTC (permalink / raw)
To: pgsql-sql
Andreas wrote:
> I'd like to update a table efficiently where the relevant select-info
> is in another table that is foreign-linked.
>
> Stupid example. 2 tables:
> things (thing_id integer, name varchar(100), color varchar(100))
> inventory (item_id integer, thing_fk integer references things
> (thing_id), number)
>
> For some reason I'd want to set the number of every red item to 0.
> This inventory doesn't contain the color but the foreign key to the
> other table where the color is found.
>
> I tried
>
> UPDATE things JOIN inventory ON things.thing_id = inventory.thing_fk
> SET number = 0
> WHERE color = 'red'
>
> PSQL didn't like the JOIN though.
> It works like this:
>
> UPDATE inventory
> SET number = 0
> WHERE thing_fk IN (SELECT thing_id FROM things WHERE color = 'red')
>
> It's effective but is it efficient, too?
What about:
UPDATE things, inventory
SET number = 0
WHERE color = 'red'
AND things.thing_id = inventory.thing_fk
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: update on join ?
2007-11-22 02:58 update on join ? Andreas <maps.on@gmx.net>
@ 2007-11-22 03:38 ` Richard Broersma Jr <rabroersma@yahoo.com>
2 siblings, 0 replies; 4+ messages in thread
From: Richard Broersma Jr @ 2007-11-22 03:38 UTC (permalink / raw)
To: pgsql-sql@postgresql.org, Andreas <maps.on@gmx.net>
--- On Wed, 11/21/07, Andreas <maps.on@gmx.net> wrote:>
> UPDATE inventory
> SET number = 0
> WHERE thing_fk IN (SELECT thing_id FROM things WHERE color
> = 'red')
This is a perfectly acceptable ANSI-SQL update statement.
Here is non-ANSI update statement that you are probably after:
UPDATE Inventory
SET number = 0
FROM Things
WHERE Inventory.thing_fk = Things.thing_id
AND Things.color = 'red';
IIRC, Joe Celko referrers to this syntax as "T-SQL".
Regards,
Richard Broersma Jr.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: update on join ?
2007-11-22 02:58 update on join ? Andreas <maps.on@gmx.net>
@ 2007-11-22 03:41 ` chester c young <chestercyoung@yahoo.com>
2 siblings, 0 replies; 4+ messages in thread
From: chester c young @ 2007-11-22 03:41 UTC (permalink / raw)
To: Andreas <maps.on@gmx.net>; +Cc: pgsql-sql
> I tried
>
> UPDATE things JOIN inventory ON things.thing_id = inventory.thing_fk
> SET number = 0
> WHERE color = 'red'
>
use the cool "from" clause in the update
update things t
set number = 0
from inventory i
where t.thing_id = i.thing_fk
and i.color = 'red';
____________________________________________________________________________________
Get easy, one-click access to your favorites.
Make Yahoo! your homepage.
http://www.yahoo.com/r/hs
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2007-11-22 03:41 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-11-22 02:58 update on join ? Andreas <maps.on@gmx.net>
2007-11-22 03:34 ` Frank Bax <fbax@sympatico.ca>
2007-11-22 03:38 ` Richard Broersma Jr <rabroersma@yahoo.com>
2007-11-22 03:41 ` chester c young <chestercyoung@yahoo.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox