pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Enrico Weigelt <weigelt@metux.de>
To: pgsql-admin@postgresql.org <pgsql-admin@postgresql.org>
Subject: Re: where with NULL values are not selected
Date: Fri, 8 Jul 2005 15:31:45 +0200
Message-ID: <20050708133145.GC30486@nibiru.borg.metux.de> (raw)
In-Reply-To: <758d5e7f0507080308232fde7f@mail.gmail.com>
References: <5B025B1F39D6D4119F5700508BEEEC6603DE3D8C@srsofaioi4546.ktso.ch>
	<758d5e7f0507080308232fde7f@mail.gmail.com>

* Dawid Kuroczko <qnex42@gmail.com> wrote:

<snip>
> 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.

Thats the statement of the day ;-)
In fact, postgres does not store empty fields, so you can save
space in long living and large tables (ie. journals) by setting
unneeded fields to NULL.
 
> 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.

better: add the missing case (A and B are NULL) to the
where clause:

SELECT ... FROM table1, table2 WHERE
    (table1.column=table2.column) OR 
    ((table1.column IS NULL) AND 
     (table2.column IS NULL));
     

cu
-- 
---------------------------------------------------------------------
 Enrico Weigelt    ==   metux IT service
  phone:     +49 36207 519931         www:       http://www.metux.de/
  fax:       +49 36207 519932         email:     contact@metux.de
---------------------------------------------------------------------
  Realtime Forex/Stock Exchange trading powered by postgresSQL :))
                                            http://www.fxignal.net/
---------------------------------------------------------------------



view thread (5+ messages)  latest in thread

Message-ID: <20050708133145.GC30486@nibiru.borg.metux.de>
Permalink:  ../20050708133145.GC30486@nibiru.borg.metux.de/
Also on:    postgresql.org/message-id/20050708133145.GC30486@nibiru.borg.metux.de

 · 

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: weigelt@metux.de
  Subject: Re: where with NULL values are not selected
  In-Reply-To: <20050708133145.GC30486@nibiru.borg.metux.de>
  References: <5B025B1F39D6D4119F5700508BEEEC6603DE3D8C@srsofaioi4546.ktso.ch> <758d5e7f0507080308232fde7f@mail.gmail.com> <20050708133145.GC30486@nibiru.borg.metux.de>

* 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