Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guhI1-0004MU-Um for pgsql-sql@arkaria.postgresql.org; Fri, 15 Feb 2019 17:28:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1guhI0-0000sG-ND for pgsql-sql@arkaria.postgresql.org; Fri, 15 Feb 2019 17:28:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guhI0-0000s7-Gn for pgsql-sql@lists.postgresql.org; Fri, 15 Feb 2019 17:28:40 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guhHy-0000pt-CC for pgsql-sql@lists.postgresql.org; Fri, 15 Feb 2019 17:28:40 +0000 Received: from [192.168.127.1] (port=25395 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1guhHw-000Oxh-Eh; Fri, 15 Feb 2019 17:28:36 +0000 Received: from [127.0.0.1] (port=58345 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1guhHv-000Ekr-Kp; Fri, 15 Feb 2019 17:28:35 +0000 From: Andrew Gierth To: Ian Tan Cc: pgsql-sql@lists.postgresql.org Subject: Re: Help on SQL query In-Reply-To: (Ian Tan's message of "Fri, 15 Feb 2019 17:22:31 +0000") Message-ID: <87k1i1klp2.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 15 Feb 2019 17:28:34 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Ian" == Ian Tan 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)