Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 68995633DB3 for ; Fri, 10 Jul 2009 05:14:40 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 74949-04 for ; Fri, 10 Jul 2009 05:14:25 -0300 (ADT) X-Greylist: delayed 00:06:38.904614 by SQLgrey-1.7.6 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by mail.postgresql.org (Postfix) with SMTP id EAE8C6325AB for ; Fri, 10 Jul 2009 05:14:28 -0300 (ADT) Received: (qmail invoked by alias); 10 Jul 2009 08:07:47 -0000 Received: from i5882CE1B.versanet.de (EHLO [192.168.0.113]) [88.130.206.27] by mail.gmx.net (mp040) with SMTP; 10 Jul 2009 10:07:47 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX1/KINYGDp/KGOpX11r08siZzSj/ROc7nJbEvI1FwM s8fU1uI1EeAi5d Message-ID: <4A56F6D2.7070802@gmx.net> Date: Fri, 10 Jul 2009 10:07:46 +0200 From: Andreas User-Agent: Thunderbird 2.0.0.22 (Windows/20090605) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: How update a table within a join efficiently ? Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.59 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200907/53 X-Sequence-Number: 32794 Hi, how would I update a table within a join in a more efficient way? E.g. the folowing case: table_a holds abstract elements. One column represents "priority" which can be based on information of other tables. table_b might hold such details in a column "size" for about 3000 of 80000 records out of table_a. I'd like to do this: UPDATE table_a SET table_a.prio = CASE WHEN size >= 10 THEN 1 ELSE 2 END FROM table_a JOIN table_b USING (table_a_id) This doesn't work. But the folowing does, though it looks not efficient with those 3000 SELECTs instead of one preparing JOIN that fetches the relevant info. :( UPDATE table_a SET prio = ( SELECT CASE WHEN size >= 10 THEN 1 ELSE 2 END FROM table_b WHERE table_a.table_a_id = table_b.table_a_id ) WHERE table_a_id IN (SELECT table_a_id FROM table_b); Is there a better way?