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 1iC4Za-0002F9-CI for pgsql-general@arkaria.postgresql.org; Sun, 22 Sep 2019 16:18:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iC4ZY-00086I-B4 for pgsql-general@arkaria.postgresql.org; Sun, 22 Sep 2019 16:18:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iC4ZW-00084U-Fa for pgsql-general@lists.postgresql.org; Sun, 22 Sep 2019 16:18:52 +0000 Received: from [66.111.4.27] (helo=out3-smtp.messagingengine.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iC4ZP-0001a0-EA for pgsql-general@lists.postgresql.org; Sun, 22 Sep 2019 16:18:49 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 3EAD221785; Sun, 22 Sep 2019 12:18:32 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sun, 22 Sep 2019 12:18:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=9 wRp2xWSOVKOCSB+l958u/SfSh+XZiZdu0qdYnaIdMc=; b=biK0lW8cc5eiUSB9A ovzEZJp5+KTG33JeN4dPXQocRY0KQlAITzdoz+ip2NBohy8Jj4FCj7/qMcZQClh1 jEWnsuVt3vY//uG1ar5xDYpSsnjctyet4xPiwBBGOXvDh6bdDpgnnNIzHVbizSgI lPHTDMLbwNIlU3Gb8KdKGjLz3gDzNtHblmCxm8dGhI4Tv7xQFWfizHn2ZEbkTla0 EeeOcTHGFA6jbZc8YoLmdXj0SomWvb6rfjmqOYU3Wq9YtdiM5SZOik5PxLNDXtYq 7FXZi4vshCj0KrdAohycwuBOz6ylTIj9NAi020u+N4JUg8lBHFTW+Qh6MySumNfP SkbPw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=9wRp2xWSOVKOCSB+l958u/SfSh+XZiZdu0qdYnaId Mc=; b=iNp74GC3lx07i3EFbjBtnGqcbBipLKS/TLLc4JfQIFKHBt+q7S0RcHnJ2 cbITirD8ok0glZE02cdLgQCwsi55sR8qxNGHv7qyL+rwo9LqtpdAsjAKX6LwX+Fa eubylTPr2V9G61QxDiQ/oRvB2FUkwKZvmV5tRFOssmVFY9S2iN7qFgL1Bz0MTG32 iuenxueAyB1AMn6HtzP7zB/8tc9m9OZQmqIzT5VAw6cnFv3Mmy63LAmRQKpoRB+3 NtJISFkAGiDmJv1aQBlW1k7bE4IXZgqS3/TmBQZguTvs9IMBOTfEhkGAtBNuZT7a 9kyT/viIf9FrB5b/Z02viWpVi88hQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvdeigdelkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepudejgedrvddurdejkedrudeike enucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgr vhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.3] (174-21-78-168.tukw.qwest.net [174.21.78.168]) by mail.messagingengine.com (Postfix) with ESMTPA id 5351C80063; Sun, 22 Sep 2019 12:18:31 -0400 (EDT) Subject: Re: Extend inner join to fetch not yet connected rows also To: Arup Rakshit , Jan Kohnert Cc: pgsql-general@lists.postgresql.org References: <7B8CBECF-09A5-492B-9E22-EDD7B0DBD469@zeit.io> <1780284.qfRisySgFs@kohni-mobil> <1695753.vyHXCZFGd1@kohni-mobil> <5C7E04B7-F5E3-4F2C-99A7-A8842153F531@zeit.io> From: Adrian Klaver Message-ID: Date: Sun, 22 Sep 2019 09:18:30 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <5C7E04B7-F5E3-4F2C-99A7-A8842153F531@zeit.io> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 66.111.4.27 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/22/19 6:30 AM, Arup Rakshit wrote: > >> On 22-Sep-2019, at 6:55 PM, Jan Kohnert wrote: >> >> Hi Arup, >> >> Am Sonntag, 22. September 2019, 14:48:20 CEST schrieb Arup Rakshit: >>> Hi Jan, >>> >>>> On 22-Sep-2019, at 5:38 PM, Jan Kohnert >>>> wrote: >>>> maybe something like >>>> >>>> select >>>> >>>> c.id, >>>> c.name, >>>> case when cs.user_id = 8 then true else false end as has >>>> >>>> from craftsmanships c >>>> left join contractor_skills cs >>>> >>>> on cs.craftsmanship_id = c.craftmanship_id; >>> >>> But this query fetched duplicate data: >> >> yeah, that's possible, since I don't exactly know your data model. If only the >> values above are required, you could simply use distinct: > > When someone adds a craftsmanship to their skill set, the contractor_skills table holds that relationship. I don’t think distinct is the correct tool, as it will eliminate the correct data. users and craftsmanship has m:n relationship via the join table contractor_skills. > > SELECT > craftsmanships.id, > craftsmanships.name, > CASE WHEN contractor_skills.user_id IS NULL THEN > FALSE > ELSE > TRUE > END AS has > FROM > "craftsmanships" > LEFT JOIN "contractor_skills" ON "contractor_skills"."craftsmanship_id" = "craftsmanships"."id" > LEFT JOIN "users" ON "users"."id" = "contractor_skills"."user_id" > WHERE (contractor_skills.user_id = 8 > OR contractor_skills.user_id IS NULL) > ORDER BY > "craftsmanships"."id”; > > Gives correct result. Not sure if still this query has bug in it. What I see is that the rows below with 'has' = 'f' will not have a user_id(implied). So I am not sure how you plan to associate that data with a user? > > id | name | has > ----+---------------------------------------+----- > 1 | paint | t > 2 | drywall | t > 3 | bathrooms | f > 4 | kitchens | f > 5 | flooring | f > 6 | basements | f > 7 | carpentry | f > 8 | decks (displayed as decks and patios) | f > 9 | windows (windows and doors) | f > 10 | countertops | f > 11 | landscaping | f > 12 | electrical | f > 13 | plumbing | f > 14 | handyman | f > (14 rows) > > > > >> >> select distinct >> c.id, >> c.name, >> case when cs.user_id = 8 then true else false end as has >> from craftsmanships c >> left join contractor_skills cs >> on cs.craftsmanship_id = c.id >> order by >> c.id; >> >> -- >> MfG Jan >> >> >> >> > > > > -- Adrian Klaver adrian.klaver@aklaver.com