Re: where with NULL values are not selected - Mailing list pgsql-admin

From Dawid Kuroczko
Subject Re: where with NULL values are not selected
Date
Msg-id 758d5e7f0507080308232fde7f@mail.gmail.com
Whole thread Raw
In response to where with NULL values are not selected  (Düster Horst <Horst.Duester@bd.so.ch>)
Responses Re: where with NULL values are not selected  (Enrico Weigelt <weigelt@metux.de>)
List pgsql-admin
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

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster


pgsql-admin by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: where with NULL values are not selected
Next
From: Enrico Weigelt
Date:
Subject: Re: where with NULL values are not selected