Re: Help on SQL query - Mailing list pgsql-sql

From Andrew Gierth
Subject Re: Help on SQL query
Date
Msg-id 87k1i1klp2.fsf@news-spur.riddles.org.uk
Whole thread Raw
In response to Help on SQL query  (Ian Tan <iantan1268@gmail.com>)
Responses Re: Help on SQL query  (Rob Sargent <robjsargent@gmail.com>)
List pgsql-sql
>>>>> "Ian" == Ian Tan <iantan1268@gmail.com> writes:

 Ian> How do I write an SQL query so that the "name" text in car_parts
 Ian> are added to the bill_of_materials table, so that it looks like
 Ian> this:

The only trick with this is that you need to join the car_parts table
twice (once for parent and once for child), and to do that you need to
give it different alias names:

select bom.parent_id,
       ppart.name as parent_name,
       bom.child_id,
       cpart.name as child_name
  from bill_of_materials bom
  join car_parts ppart on (ppart.id=bom.parent_id)
  join car_parts cpart on (cpart.id=bom.child_id);

-- 
Andrew (irc:RhodiumToad)


pgsql-sql by date:

Previous
From: Ian Tan
Date:
Subject: Help on SQL query
Next
From: Rob Sargent
Date:
Subject: Re: Help on SQL query