Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hw8Ka-00064Y-OP for pgsql-general@arkaria.postgresql.org; Fri, 09 Aug 2019 17:05:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hw8KZ-0004x0-Fw for pgsql-general@arkaria.postgresql.org; Fri, 09 Aug 2019 17:05:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hw8KZ-0004wX-6K for pgsql-general@lists.postgresql.org; Fri, 09 Aug 2019 17:05:31 +0000 Received: from evolu-s.it ([94.23.66.144] helo=smtp.evolu-s.it) by magus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1hw8KQ-0007sH-Mo for pgsql-general@postgresql.org; Fri, 09 Aug 2019 17:05:30 +0000 Received: from [192.168.1.131] ([79.6.30.247]) by smtp.evolu-s.it ; Fri, 9 Aug 2019 19:05:18 +0200 Subject: Re: Updating 3-table dataset To: Luca Ferrari Cc: PostgreSQL mailing lists References: <70caca24-0f9e-e9e6-56d0-2be96e6d9be8@evolu-s.it> From: Moreno Andreo Message-ID: <8fe49799-a2a3-ff36-dbca-432363f9a26b@evolu-s.it> Date: Fri, 9 Aug 2019 19:05:25 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: it List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Il 09/08/19 16:50, Luca Ferrari ha scritto: > On Fri, Aug 9, 2019 at 2:29 PM Moreno Andreo wrote: >> Hi all, >> I don't know if that's the heat burning my brain but I can't find a >> solution to what seemed a simple operation to me. >> >> I have 3 tables >> create table t_all >> { >> id uuid, >> ref_id uuid (FK to t_ana.id) >> }; >> create table t_ana >> { >> id uuid, >> code text >> }; >> create table t_app >> { >> id uuid, >> code text (subset of t_ana.code) >> } >> I need to update t_all set t_all.id = t_app.id having t_ana.code in >> t_app.code (I wrote it in some kind of meta-sql but I hope it's clear) >> I tried to create a view but I need an INSTEAD OF trigger, since it >> spreads among 3 tables so I hope there's some faster path to achieve the >> solution.... >> > Not sure I got what you need, and I've not tested, but something like > the following: > > WITH must_update AS ( > SELECT app.id AS app_id, ana.id AS ana_id > FROM t_app app, t_ana ana > WHERE app.code = ana.code > ) > > UPDATE t_all > SET id = ( SELECT app_id FROM must_update WHERE ref_id = must_update.ana_id ); > > I've written the CTE because it is a little clearer in my mind, but > you can push down as a subquery of course. Thanks Luca, that's the idea I needed... now some small trimming and I think I'll be there (just for the logs... the UPDATE statement needs a WHERE clause, otherwise it will NULL the id field in all rows where ref_id is not present in must_update :-) ) I owe you a beer :-) Cheers Moreno. > > Luca >