Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 1DD83B5D942 for ; Sat, 14 May 2011 19:09:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 22971-05 for ; Sat, 14 May 2011 22:09:20 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id ECB57B5D91F for ; Sat, 14 May 2011 19:09:17 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QLN1P-0003oe-EK for pgsql-sql@postgresql.org; Sun, 15 May 2011 00:09:15 +0200 Received: from s01060015e975d7fb.wp.shawcable.net ([24.77.73.195]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 15 May 2011 00:09:15 +0200 Received: from spluque by s01060015e975d7fb.wp.shawcable.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 15 May 2011 00:09:15 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Seb Subject: self join Date: Sat, 14 May 2011 17:09:03 -0500 Organization: Church of Emacs Lines: 47 Message-ID: <87y629rl34.fsf@kolob.subpolar.dyndns.org> Mime-Version: 1.0 Content-Type: text/plain X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: s01060015e975d7fb.wp.shawcable.net User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:SOQhJeuS7Lv4jWSuIRa96Cud2m0= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, T_RP_MATCHES_RCVD=-0.01, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201105/91 X-Sequence-Number: 35375 Hi, This probably reflects my confusion with how self joins work. Suppose we have this table: =# SELECT * FROM tmp; a | b ---+--- 1 | 2 2 | 3 4 | 5 (3 rows) If I want to get a table with records where none of the values in column b are found in column a, I thought this should do it: =# SELECT * FROM tmp t1, tmp t2 WHERE t2.b <> t1.a; a | b | a | b ---+---+---+--- 1 | 2 | 1 | 2 1 | 2 | 2 | 3 1 | 2 | 4 | 5 2 | 3 | 2 | 3 2 | 3 | 4 | 5 4 | 5 | 1 | 2 4 | 5 | 2 | 3 4 | 5 | 4 | 5 (8 rows) I need to get: a | b | a | b ---+---+---+--- 1 | 2 | 1 | 2 4 | 5 | 4 | 5 Or just: a | b ---+--- 1 | 2 4 | 5 -- Seb