Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1enhfm-0001NX-FR for pgsql-general@arkaria.postgresql.org; Mon, 19 Feb 2018 09:23:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1enhfl-0006iJ-KN for pgsql-general@arkaria.postgresql.org; Mon, 19 Feb 2018 09:23:45 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1enhfl-0006i9-Cd for pgsql-general@lists.postgresql.org; Mon, 19 Feb 2018 09:23:45 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1enhfh-0001g5-O7 for pgsql-general@postgresql.org; Mon, 19 Feb 2018 09:23:44 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 2EA6A42BC5; Mon, 19 Feb 2018 10:23:40 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id IKvGT7VaKLTl; Mon, 19 Feb 2018 10:23:38 +0100 (CET) Message-ID: <1519032216.2503.5.camel@cybertec.at> Subject: Re: Join query From: Laurenz Albe To: hmidi slim , pgsql-general@postgresql.org Date: Mon, 19 Feb 2018 10:23:36 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk hmidi slim wrote: > I have two tables: establishment which contains these columns: id, name, longitude, latitude, geom (Geometric column) > Product contains: id, name, establishment_id > First of all I want to select the establishment within a radius. > I run this query: > select e.name, e1.name > from establishment as e, establishment as e1 > where e.id <> e1.id > and e1.id = 1 > and ST_DWithin(geom, ST_MakePoint(e1.longitude, e1.latitude)::geography, 1000) > > The result of this query returns all the establishment within a radius 1KM from from a given establishment which has an id = 1. > > After that I want to get the product's name of each establishment from the query's result. > > Is there an other optimized solution to make a query such this: > select * from ( > select e.name, e1.name, e.id > from establishment as e, establishment as e1 > where e.id <> e1.id > and e1.id = 1 > and ST_DWithin(geom, ST_MakePoint(e1.longitude, e1.latitude)::geography, 1000)) as tmp inner join product as p on p.establishment_id = tmp.id A simple join is what you need: SELECT e.name, e1.name FROM establishment AS e JOIN establishment AS e1 ON ST_DWithin(e.geom, ST_MakePoint(e1.longitude, e1.latitude)::geography, 1000) AND e.id <> e1.id JOIN product AS p ON p.establishment_id = e.id WHERE e1.id = 1; Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com