X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 6DEEB52A71 for ; Fri, 8 Jul 2005 11:32:49 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 38405-06 for ; Fri, 8 Jul 2005 14:32:43 +0000 (GMT) Received: from metux.de (seven.metux.de [193.16.1.1]) by svr1.postgresql.org (Postfix) with ESMTP id 63E6552858 for ; Fri, 8 Jul 2005 11:32:41 -0300 (ADT) Received: (from weigelt@localhost) by metux.de (8.12.10/8.12.10) id j68DVjxW005422 for pgsql-admin@postgresql.org; Fri, 8 Jul 2005 15:31:45 +0200 Date: Fri, 8 Jul 2005 15:31:45 +0200 From: Enrico Weigelt To: "pgsql-admin@postgresql.org" Subject: Re: where with NULL values are not selected Message-ID: <20050708133145.GC30486@nibiru.borg.metux.de> Reply-To: weigelt@metux.de Mail-Followup-To: "pgsql-admin@postgresql.org" References: <5B025B1F39D6D4119F5700508BEEEC6603DE3D8C@srsofaioi4546.ktso.ch> <758d5e7f0507080308232fde7f@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <758d5e7f0507080308232fde7f@mail.gmail.com> User-Agent: Mutt/1.4.1i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.235 tagged_above=0 required=5 tests=AWL, TO_ADDRESS_EQ_REAL X-Spam-Level: X-Archive-Number: 200507/62 X-Sequence-Number: 18118 * Dawid Kuroczko wrote: > 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/ ---------------------------------------------------------------------