Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 7A6702E0035 for ; Wed, 21 Nov 2007 23:34:40 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 30964-05 for ; Wed, 21 Nov 2007 23:34:32 -0400 (AST) Received: from tomts13-srv.bellnexxia.net (tomts13.bellnexxia.net [209.226.175.34]) by postgresql.org (Postfix) with ESMTP id 96E0B2E0033 for ; Wed, 21 Nov 2007 23:34:33 -0400 (AST) Received: from toip5.srvr.bell.ca ([209.226.175.88]) by tomts13-srv.bellnexxia.net (InterMail vM.5.01.06.13 201-253-122-130-113-20050324) with ESMTP id <20071122033427.NVEO13659.tomts13-srv.bellnexxia.net@toip5.srvr.bell.ca> for ; Wed, 21 Nov 2007 22:34:27 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ah4FADuHREdBX6dI/2dsb2JhbACBW48g Received: from bas3-london14-1096787784.dsl.bell.ca (HELO t60p.bax.ca) ([65.95.167.72]) by toip5.srvr.bell.ca with ESMTP; 21 Nov 2007 22:36:54 -0500 Message-ID: <4744F8C0.7040106@sympatico.ca> Date: Wed, 21 Nov 2007 22:34:24 -0500 From: Frank Bax User-Agent: Thunderbird 2.0.0.6 (X11/20070818) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: update on join ? References: <4744F06A.8080302@gmx.net> In-Reply-To: <4744F06A.8080302@gmx.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/106 X-Sequence-Number: 29634 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