Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dNaps-00071l-N7 for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jun 2017 08:18:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dNaps-0003qh-1m for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jun 2017 08:18:00 +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 1dNap8-0001JT-PF for pgsql-bugs@postgresql.org; Wed, 21 Jun 2017 08:17:14 +0000 Received: from mail-lf0-x22b.google.com ([2a00:1450:4010:c07::22b]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dNaox-0001JP-T6 for pgsql-bugs@postgresql.org; Wed, 21 Jun 2017 08:17:14 +0000 Received: by mail-lf0-x22b.google.com with SMTP id l13so29135135lfl.1 for ; Wed, 21 Jun 2017 01:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=HuVSZjr87jhWBRHbYCclPFMLnatH1pqLPWaUR74glHw=; b=PeLYmKn8RJLjXShe8Ex2zno8ZLcYe+jbXi3XNLfLO4CAQb6PwQ1GrrEauLTLPqGMjL cT0YfwKDoou19FpGzsXx2N/2LGDNa/dciVerkWjQwkO9MkD+JENTvTwz9toe+4WJ5Zaf sae4tQIHETD8f84LPhtpFPZD9ULO9pJ5Kk7VyloEhnd2NGkylJODXczACTaNX6dex8r3 RXrpkE9R5Zvo97C3ETQ7t/cTWPd8nescGRXwuxmDuOeq+k56QnJZ0W0g8rU2atEpzSGo 4JjpZTSi9u6hFNjqewNiferT6aAs5B3S2AyET6Qmhz4FmztJeHx5hQrtd+d9mhrAisfR Tj9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:references:from:message-id :date:user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=HuVSZjr87jhWBRHbYCclPFMLnatH1pqLPWaUR74glHw=; b=OEXmVJ8ICCzePxZQVIVG0Gh25/bk7M96Xb8x7bfBxp69Cl8+pUFXgt1mCKIuag51LJ 94JgMbrTJ/52a19A4FMggtKr5zQvNE//m52C/h7YgskSgfeja1wJhZBQw7bKrjfJmjwF fY9U3wbrGXYGdLtGI5gE/ka0Xj+2aXldChKjMC4ga2VrRjaRM/b2ujFNg1kjCIcFRyGu aKcnm25Efdd3P5gnL4ehlNJjmWJ403QNJyBlZxZ/sWrlicCVSghc3VHAA0tGopIBF71x oJ60qKY0LSMlXfcDzBjn0QFxUgqL7q9Gtbc7g3fqt8byEHjN1m6Ny1g8GJlFBe/tPcxo VgEA== X-Gm-Message-State: AKS2vOzfNOWE4BWjvJ559fYAnsKwAkXN3VcBk6067QlBqcoKVNnQQ7JJ 8s4sF23urC1HdDRv X-Received: by 10.25.26.206 with SMTP id a197mr11239494lfa.5.1498033022820; Wed, 21 Jun 2017 01:17:02 -0700 (PDT) Received: from [192.168.1.99] (dsl-hkibng32-54fb4e-103.dhcp.inet.fi. [84.251.78.103]) by smtp.gmail.com with ESMTPSA id 25sm2355516ljo.22.2017.06.21.01.17.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Jun 2017 01:17:02 -0700 (PDT) Subject: Re: Urgent - SQL left join bug? To: Kaijiang Chen , Pg Bugs References: From: Heikki Linnakangas Message-ID: <55f7dcea-cd54-7f4c-e2e2-efc0e5e1cdfa@iki.fi> Date: Wed, 21 Jun 2017 11:17:01 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On 06/21/2017 11:06 AM, Kaijiang Chen wrote: > I ran a SQL: > select pha.id,ph.herb_id,pha.name,ph.weight > from prescription_herbs as ph *left join* pharmacy_herbs as pha on > ph.herb_id=pha.herb_id > where ph.prescription_id=116285 and ph.deleted_at is null and > pha.deleted_at is null and pha.pharmacy_id=22 order by ph.herb_id; > > Expected: > > It should have 10 rows because the SQL "select * from prescription_herbs as > ph where ph.prescription_id=116285 and ph.deleted_at is null" returned 10 > rows and I'm using *LEFT JOIN *in the above SQL. > > Actual Result: > > It returned only 9 rows and the result is the same as JOIN (not LEFT JOIN). Note that you have the condition "pha.pharmacy_id=22" in the WHERE part of the query. That filters out rows with no matching pharmacy_herbs rows, because pha.pharmacy_id is NULL for non-matching rows. Put the "pha.pharmacy_id=22" condition in the ON join qual part instead. And for readability and consistency, I'd suggest putting the "pha.deleted_at is null" qual in the ON clause too, although that won't affect the result: select pha.id,ph.herb_id,pha.name,ph.weight from prescription_herbs as ph left join pharmacy_herbs as pha on ph.herb_id=pha.herb_id and pha.deleted_at is null and pha.pharmacy_id=22 where ph.prescription_id=116285 and ph.deleted_at is null order by ph.herb_id; - Heikki -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs