pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dawid Kuroczko <qnex42@gmail.com>
To: Düster Horst <Horst.Duester@bd.so.ch>
Cc: pgsql-admin@postgresql.org <pgsql-admin@postgresql.org>
Subject: Re: where with NULL values are not selected
Date: Fri, 8 Jul 2005 12:08:25 +0200
Message-ID: <758d5e7f0507080308232fde7f@mail.gmail.com> (raw)
In-Reply-To: <5B025B1F39D6D4119F5700508BEEEC6603DE3D8C@srsofaioi4546.ktso.ch>
References: <5B025B1F39D6D4119F5700508BEEEC6603DE3D8C@srsofaioi4546.ktso.ch>

On 7/8/05, Düster Horst <Horst.Duester@bd.so.ch> wrote:
> I try to join two tables whereas some column values do have NULL values with
> the following query:
> 
> select table1.column from table1, table2 where table1.column=table2.column
> 
> table1.column and table2.column may have NULL values. The problem is that
> these columns where not selected. Does there exists any solution to
> select/join the NULL value colums also.

A hint:
qnex=# SELECT 'ok' WHERE NULL=NULL;
?column?
(0 rows)

In other words -- NULL is not equal to NULL.  NULL is not a value, NULL
is a state.  If you want to join those columns, you cannot use NULL
as a joining key.  It's the way SQL works.

Anyway, a quick-and-dirty solution might be:

select table1.column from table1, table2 where
coalesce(table1.column,-1)=coalesce(table2.column, -1);

...assuming column doesn't have '-1' value.

A better solution would be to rethink your design (don't use NULLs).


Incientally, instead of using this syntax:
select table1.column from table1, table2 where table1.column=table2.column;
...try using explicit inner joins:
select table1.column from table1 JOIN table2 USING(column);

Regards,
    Dawid



view thread (5+ messages)  latest in thread

Message-ID: <758d5e7f0507080308232fde7f@mail.gmail.com>
Permalink:  ../758d5e7f0507080308232fde7f@mail.gmail.com/
Also on:    postgresql.org/message-id/758d5e7f0507080308232fde7f@mail.gmail.com

 · 

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-admin@postgresql.org
  Cc: qnex42@gmail.com, Horst.Duester@bd.so.ch
  Subject: Re: where with NULL values are not selected
  In-Reply-To: <758d5e7f0507080308232fde7f@mail.gmail.com>
  References: <5B025B1F39D6D4119F5700508BEEEC6603DE3D8C@srsofaioi4546.ktso.ch> <758d5e7f0507080308232fde7f@mail.gmail.com>

* 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