pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: where with NULL values are not selected
5+ messages / 4 participants
[nested] [flat]

* Re: where with NULL values are not selected
@ 2005-07-08 09:54  Peter Eisentraut <peter_e@gmx.net>
  parent: Düster Horst <Horst.Duester@bd.so.ch>
  1 sibling, 0 replies; 5+ messages in thread

From: Peter Eisentraut @ 2005-07-08 09:54 UTC (permalink / raw)
  To: pgsql-admin; +Cc: Düster Horst <Horst.Duester@bd.so.ch>

Am Freitag, 8. Juli 2005 12:08 schrieb Düster Horst:
> 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.

Read up on outer joins.  In your case, try this:

select table1.column from table1 full outer join table2 on 
(table1.column=table2.column);

-- 
Peter Eisentraut
http://developer.postgresql.org/~petere/



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: where with NULL values are not selected
@ 2005-07-08 10:08  Dawid Kuroczko <qnex42@gmail.com>
  parent: Düster Horst <Horst.Duester@bd.so.ch>
  1 sibling, 1 reply; 5+ messages in thread

From: Dawid Kuroczko @ 2005-07-08 10:08 UTC (permalink / raw)
  To: Düster Horst <Horst.Duester@bd.so.ch>; +Cc: 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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* where with NULL values are not selected
@ 2005-07-08 10:08  Düster Horst <Horst.Duester@bd.so.ch>
  0 siblings, 2 replies; 5+ messages in thread

From: Düster Horst @ 2005-07-08 10:08 UTC (permalink / raw)
  To: pgsql-admin

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.

I would be appeciate about any hint.

PostgreSQL 7.4.5

With best regards
Horst Duester



---------------------------------
 
Dr. Horst Düster 
GIS-Koordinator /Amtschef Stv.

Kanton Solothurn 
Amt für Geoinformation
Abteilung SO!GIS Koordination
Werkhofstr. 65
CH-4509 Solothurn 

Tel.: ++41 (0)32 627 25 32 
Fax: ++41 (0)32 627 22 14

horst.duester@bd.so.ch 
www.sogis.so.ch




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: where with NULL values are not selected
@ 2005-07-08 13:31  Enrico Weigelt <weigelt@metux.de>
  parent: Dawid Kuroczko <qnex42@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Enrico Weigelt @ 2005-07-08 13:31 UTC (permalink / raw)
  To: pgsql-admin

* 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/
---------------------------------------------------------------------



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: where with NULL values are not selected
@ 2005-07-11 10:28  Düster Horst <Horst.Duester@bd.so.ch>
  0 siblings, 0 replies; 5+ messages in thread

From: Düster Horst @ 2005-07-11 10:28 UTC (permalink / raw)
  To: pgsql-admin

The problem is solved. The coalesce() function was the solution. Thank you
very much for your input.

With best regards

Horst Düster

---------------------------------
 
Dr. Horst Düster 
GIS-Koordinator /Amtschef Stv.

Kanton Solothurn 
Amt für Geoinformation
Abteilung SO!GIS Koordination
Werkhofstr. 65
CH-4509 Solothurn 

Tel.: ++41 (0)32 627 25 32 
Fax: ++41 (0)32 627 22 14

horst.duester@bd.so.ch 
www.sogis.so.ch



-----Ursprüngliche Nachricht-----
Von: Dawid Kuroczko [mailto:qnex42@gmail.com]
Gesendet am: Freitag, 8. Juli 2005 11:08
An: Düster Horst
Cc: pgsql-admin@postgresql.org
Betreff: Re: [ADMIN] where with NULL values are not selected

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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2005-07-11 10:28 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-07-08 10:08 where with NULL values are not selected Düster Horst <Horst.Duester@bd.so.ch>
2005-07-08 09:54 ` Peter Eisentraut <peter_e@gmx.net>
2005-07-08 10:08 ` Dawid Kuroczko <qnex42@gmail.com>
2005-07-08 13:31   ` Enrico Weigelt <weigelt@metux.de>
2005-07-11 10:28 Re: where with NULL values are not selected Düster Horst <Horst.Duester@bd.so.ch>

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