Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f48C6kA24606 for ; Tue, 8 May 2001 08:06:46 -0400 (EDT) (envelope-from olly@lfix.co.uk) Received: from lfix.demon.co.uk ([158.152.59.127] helo=linda.lfix.co.uk) by anchor-post-31.mail.demon.net with esmtp (Exim 2.12 #1) id 14x6GF-0003ek-0V; Tue, 8 May 2001 13:06:39 +0100 Received: from lfix.co.uk (olly@localhost [127.0.0.1]) by localhost (8.12.0.Beta7/8.12.0.Beta7/Debian 8.12.0.Beta7-1) with ESMTP id f48BT8iW006285; Tue, 8 May 2001 12:29:10 +0100 Message-Id: <200105081129.f48BT8iW006285@linda.lfix.co.uk> X-Mailer: exmh version 2.3.1 01/18/2001 (debian 2.3.1-1) with nmh-1.0.4+dev X-URL: http://www.lfix.co.uk/oliver X-face: "xUFVDj+ZJtL_IbURmI}!~xAyPC"Mrk=MkAm&tPQnNq(FWxv49R}\>0oI8VM?O2VY+N7@F- KMLl*!h}B)u@TW|B}6 cc: pgsql-sql@postgresql.org Subject: Re: left join syntax In-reply-to: Message from "Haywood J'Bleauxmie" of Tue, 08 May 2001 02:36:14 CDT. Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Tue, 08 May 2001 12:29:08 +0100 From: "Oliver Elphick" X-Archive-Number: 200105/56 X-Sequence-Number: 2857 "Haywood J'Bleauxmie" wrote: >I have a database that tracks work orders. Each order tracks two entries >from the employees table; the employee ID of the person assigned to the >work order and the ID of the person who completed the order. Each work >order may have one, both, or neither field filled in. As such, I need to >left join the employee table to the work order table, but I cannot figure >out the syntax for the double-join. As independent selects, I can do the >join: > >SELECT o.ordr_id, a.last_name >FROM ordr o left join employee a on o.assigned_id = a.emp_id; > >SELECT o.ordr_id, c.last_name >FROM ordr o left join employee c on o.completion_id = c.emp_id; > >But I would like to have the whole thing in a single SELECT. Can you help >me out? Just combine them: junk=# select * from ordr; ordr_id | assigned_id | completion_id ---------+-------------+--------------- 1 | | 2 | 1 | 3 | 1 | 2 4 | | 2 (4 rows) junk=# select * from employee; emp_id | last_name --------+----------- 1 | aaa 2 | bbb 3 | ccc (3 rows) junk=# SELECT o.ordr_id, a.last_name AS assigned, c.last_name AS completion junk-# FROM ordr AS o junk-# LEFT JOIN employee AS a ON o.assigned_id = a.emp_id junk-# LEFT JOIN employee AS c ON o.completion_id = c.emp_id junk-# ORDER BY ordr_id; ordr_id | assigned | completion ---------+----------+------------ 1 | | 2 | aaa | 3 | aaa | bbb 4 | | bbb (4 rows) -- Oliver Elphick Oliver.Elphick@lfix.co.uk Isle of Wight http://www.lfix.co.uk/oliver PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47 6B 7E 39 CC 56 E4 C1 47 GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C ======================================== "Follow peace with all men, and holiness, without which no man shall see the Lord." Hebrews 12:14