Re: SQL question

From: Richard Huxton <dev(at)archonet(dot)com>
To: Együd Csaba <csegyud(at)freemail(dot)hu>, pgsql-general(at)postgresql(dot)org
Subject: Re: SQL question
Date: 2003-06-19 18:37:40
Message-ID: 200306191937.40883.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Ayers 2003-06-19 18:50:44 Re: Performance differences using varchar, char and text
Previous Message Alvaro Herrera 2003-06-19 18:28:01 Re: Finding Current Page Size

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeff Eckermann 2003-06-19 19:23:14 Re: Is there any way to make post to newsgroups faster ?
Previous Message Jeroen T. Vermeulen 2003-06-19 16:50:18 Re: Access to transaction status