pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Együd Csaba <csegyud@freemail.hu>
To: pgsql-general@postgresql.org
Subject: Re: SQL question
Date: Thu, 19 Jun 2003 19:37:40 +0100
Message-ID: <200306191937.40883.dev@archonet.com> (raw)
In-Reply-To: <000801c33680$e7f56a00$230a0a0a@compaq>
References: <Pine.LNX.4.33.0306050954240.16550-100000@css120.ihs.com>
	<000801c33680$e7f56a00$230a0a0a@compaq>

On Thursday 19 Jun 2003 5:36 pm, Együd Csaba wrote:
> Hi All,
> I have a problem. I have 3 tables.
> 1. stock changes
> 2. product groups
> 3. a link table between the 2 above
>
> I need the name of the product group the product belongs to, which product
> is the subject of the stock change. (I hope it's understandable)
>
> So I tried this query:
> ---------------------------------------------------------------------------
>- ------------------
> DB=# select t_stockchanges.productid, (select name from t_productgroups
> where id=(select productgroupid from t_prod_in_pgr where
> productid=t_stockchanges.productid)) as pgroup from t_stockchanges;

Try something like:

SELECT chg.productid, grp.name as pgroup
FROM t_stockchanges chg, t_prod_in_pgr pp, t_productgroups grp
WHERE chg.productid=pp.productid AND pp.productgroupid=grp.id;

I might have got some of your fields wrong, but what I'm trying to do is join 
across the linked fields.

change.product_id => linktbl.product_id, linktbl.group_id => groups.group_id

No need for a subselect here.
-- 
  Richard Huxton



view thread (71+ messages)  latest in thread

Message-ID: <200306191937.40883.dev@archonet.com>
Permalink:  ../200306191937.40883.dev@archonet.com/
Also on:    postgresql.org/message-id/200306191937.40883.dev@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: dev@archonet.com, csegyud@freemail.hu
  Subject: Re: SQL question
  In-Reply-To: <200306191937.40883.dev@archonet.com>
  References: <Pine.LNX.4.33.0306050954240.16550-100000@css120.ihs.com> <000801c33680$e7f56a00$230a0a0a@compaq> <200306191937.40883.dev@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox