Re: Query Advice

From: Vincent Elschot <vinny(at)xs4all(dot)nl>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Query Advice
Date: 2017-03-30 18:16:58
Message-ID: dde4b09c-cccf-7340-0705-5eb64d010c7e@xs4all.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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?

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Rob Sargent 2017-03-30 18:17:20 Re: Query Advice
Previous Message David G. Johnston 2017-03-30 18:13:31 Re: Query Advice