Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 097B22E46FA for ; Thu, 10 Jan 2008 18:22:52 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 27839-02 for ; Thu, 10 Jan 2008 18:22:48 -0400 (AST) Received: from smtpauth.rollernet.us (smtpauth.rollernet.us [208.11.75.5]) by postgresql.org (Postfix) with ESMTP id D7BC12E46F1 for ; Thu, 10 Jan 2008 18:22:48 -0400 (AST) Received: from smtpauth.rollernet.us (localhost.localdomain [127.0.0.1]) by smtpauth.rollernet.us (Postfix) with ESMTP id 0979F59400A; Thu, 10 Jan 2008 14:22:46 -0800 (PST) Received: from iron.denterprises.org (ool-4350518c.dyn.optonline.net [67.80.81.140]) by smtpauth.rollernet.us (Postfix) with ESMTP; Thu, 10 Jan 2008 14:22:45 -0800 (PST) Received: from [127.0.0.1] (kell.iron.denterprises.org [192.168.171.80]) by iron.denterprises.org (Postfix) with ESMTP id AD131167660; Thu, 10 Jan 2008 17:22:44 -0500 (EST) Message-ID: <47869A9C.7010203@denterprises.org> Date: Thu, 10 Jan 2008 17:22:20 -0500 From: Colin Wetherbee User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: Paul Lambert CC: pgsql-sql@postgresql.org Subject: Re: JOIN a table twice for different values in the same query References: <47869704.8050306@denterprises.org> <478699D0.7070608@optusnet.com.au> In-Reply-To: <478699D0.7070608@optusnet.com.au> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 080109-0, 01/09/2008), Outbound message X-Antivirus-Status: Clean X-Rollernet-Abuse: Processed by Roller Network Mail Services. Contact abuse@rollernet.us to report violations. Abuse policy: http://rollernet.us/abuse.php X-Rollernet-Submit: Submit ID 350d.47869ab5.c2d19.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/80 X-Sequence-Number: 29933 Paul Lambert wrote: > Colin Wetherbee wrote: >> I would like to construct a query on the flight table that returns the >> names of both the departure port and the arrival port. >> >> The following query shows how I would get just the departure port. >> >> js=# SELECT departure_date, jsports.code AS departure_code FROM >> jsjourneys JOIN jsports ON jsjourneys.departure_port = jsports.id LIMIT >> 4; >> > Try joining twice, something like: > > SELECT departure_date, > dp.code AS departure_code, > ap.code AS arrival_code > FROM jsjourneys > JOIN jsports dp ON jsjourneys.departure_port = jsports.id > JOIN jsports ap ON jsjourneys.arrival_port=jsports.id > > ---------------------------(end of broadcast)--------------------------- > TIP 6: explain analyze is your friend Ah, I didn't realize you could alias tables inside the JOIN. Excellent. It works. :) js=# SELECT departure_date, dp.code AS departure_code, ap.code AS arrival_code FROM jsjourneys JOIN jsports dp ON jsjourneys.departure_port = dp.id JOIN jsports ap ON jsjourneys.arrival_port = ap.id LIMIT 4; departure_date | departure_code | arrival_code ----------------+----------------+-------------- 2006-11-19 | BHM | ATL 2006-11-16 | PIT | ATL 2006-11-16 | ATL | BHM 2006-10-26 | PIT | BOS (4 rows) For archive completeness, note the query is joined relative to dp.id and ap.id, rather than jsports.id. Thanks for your help! Colin