agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Gary Stainburn <gary.stainburn@ringways.co.uk>
Cc: pgsql-sql@lists.postgresql.org, Pavel Stehule <pavel.stehule@gmail.com>
Subject: Re: select where not in () fails
Date: Sat, 22 Sep 2018 19:13:03 +0100
Message-ID: <871s9lif94.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <201809211620.16084.gary.stainburn@ringways.co.uk>
References: <201809211608.41086.gary.stainburn@ringways.co.uk>
<CAFj8pRDz6x=59Y118YnKhMDvCGedt+Soj5-0dDFQMHb4AwMiCw@mail.gmail.com>
<201809211620.16084.gary.stainburn@ringways.co.uk>
>>>>> "Gary" == Gary Stainburn <gary.stainburn@ringways.co.uk> writes:
Gary> As I said in my description, some values will be NULL. I just
Gary> thought that these would not be included in the select. I did not
Gary> think that it would stop the subselect from working
https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_NOT_IN
Gary> users=# select count(u_id) from users where u_id not in (select
Gary> distinct emp_u_id from employees where emp_u_id is not null);
Never use DISTINCT inside IN; the IN already implies it.
Always rewrite NOT IN (select ...) to use NOT EXISTS instead, like so:
select count(u_id) from users u
where not exists (select 1 from employees e where u.u_id=e.emp_u_id);
(and always qualify every column reference in the query, especially when
using IN)
--
Andrew (irc:RhodiumToad)
view thread (5+ messages)
Message-ID: <871s9lif94.fsf@news-spur.riddles.org.uk>
Permalink: ../871s9lif94.fsf@news-spur.riddles.org.uk/
Also on: postgresql.org/message-id/871s9lif94.fsf@news-spur.riddles.org.uk
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: andrew@tao11.riddles.org.uk, gary.stainburn@ringways.co.uk, pavel.stehule@gmail.com
Subject: Re: select where not in () fails
In-Reply-To: <871s9lif94.fsf@news-spur.riddles.org.uk>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox