Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cted5-0008F1-Tv for pgsql-sql@arkaria.postgresql.org; Thu, 30 Mar 2017 18:17:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cted5-0005pY-GX for pgsql-sql@arkaria.postgresql.org; Thu, 30 Mar 2017 18:17:03 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cted3-0005n6-Qa for pgsql-sql@postgresql.org; Thu, 30 Mar 2017 18:17:01 +0000 Received: from lb1-smtp-cloud2.xs4all.net ([194.109.24.21]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cted1-0000jj-EX for pgsql-sql@postgresql.org; Thu, 30 Mar 2017 18:17:01 +0000 Received: from [10.63.6.222] ([212.178.216.14]) by smtp-cloud2.xs4all.net with ESMTP id 2JGw1v00R0KD9cL01JGyTz; Thu, 30 Mar 2017 20:16:58 +0200 Subject: Re: Query Advice To: pgsql-sql@postgresql.org References: From: Vincent Elschot Message-ID: Date: Thu, 30 Mar 2017 20:16:58 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Op 30/03/2017 om 20:03 schreef Gary Chambers: > All, > > Given the following tables: > > company > ------- > company_id > name > > postal_addresses > ---------------- > postal_address_id > company_id > description > addr > city > stprov > zip > > > I've been handling joins as such: > > select c.company_id, > array(select concat_ws('|', pa.description, pa.addr, pa.city, > pa.stprov, pa.zip) > ) addrs > from companies c inner join postal_addresses pa using (company_id) > where company_id = 1731; > > Is there a better way to get the company information along with all of > the > addresses in a single query? This works, but it requires the additional > step of splitting the addresses by the the delimiter at the application > layer. > > Thanks for any advice you have. > > -- > G. > > "better" depends very much on your needs. I tend to return this kind of data as a JSON string because python (django) can be instructed to automatically translate that into an array that I can loop through. Do you have any particular reason for wanting to do this in one query, given that you seem to want a regular resultset for the addresses? -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql