pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Kaijiang Chen <chenkaijiang@gmail.com>
To: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: Urgent - SQL left join bug?
Date: Wed, 21 Jun 2017 11:17:01 +0300
Message-ID: <55f7dcea-cd54-7f4c-e2e2-efc0e5e1cdfa@iki.fi> (raw)
In-Reply-To: <CAAkGvS9htFwyOPfy__sKaBRJDZoqthH6vzM_-OtmmThcJT7k2A@mail.gmail.com>
References: <CAAkGvS9htFwyOPfy__sKaBRJDZoqthH6vzM_-OtmmThcJT7k2A@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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



view thread (7+ messages)  latest in thread

Message-ID: <55f7dcea-cd54-7f4c-e2e2-efc0e5e1cdfa@iki.fi>
Permalink:  ../55f7dcea-cd54-7f4c-e2e2-efc0e5e1cdfa@iki.fi/
Also on:    postgresql.org/message-id/55f7dcea-cd54-7f4c-e2e2-efc0e5e1cdfa@iki.fi

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: hlinnaka@iki.fi, chenkaijiang@gmail.com
  Subject: Re: Urgent - SQL left join bug?
  In-Reply-To: <55f7dcea-cd54-7f4c-e2e2-efc0e5e1cdfa@iki.fi>
  References: <CAAkGvS9htFwyOPfy__sKaBRJDZoqthH6vzM_-OtmmThcJT7k2A@mail.gmail.com> <55f7dcea-cd54-7f4c-e2e2-efc0e5e1cdfa@iki.fi>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox