Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 89335B5DBEE for ; Sat, 14 May 2011 19:39:14 -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 40357-01 for ; Sat, 14 May 2011 22:39:07 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-gx0-f174.google.com (mail-gx0-f174.google.com [209.85.161.174]) by mail.postgresql.org (Postfix) with ESMTP id 8F83DB5DBED for ; Sat, 14 May 2011 19:39:07 -0300 (ADT) Received: by gxk21 with SMTP id 21so1210000gxk.19 for ; Sat, 14 May 2011 15:39:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=6FW9rOe5acym38UDM4xquujnSkC2k1JYJqNBNQZKuN8=; b=Zm4HOTCBzHU/D+OEi0dXYgp7HVcznpXNMCxwtNNvbgtIfsKTpk+SHtPU8mibZ1xmvA qj5m3fwY3farG9O/ayluykrzyQcIGkdfvncYmeMC349qnMC1GaMA1XT7ujZ05GpmZNeK /KbP0FdZ0pOwnCPB06b8k2w9hksHDoydFx1rk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=bkgkJ0wo259Bbn9WtbS0eCFkvpEPNYpbU8MvQgCBF3qAh9XhbvjO05WTg1DlUFUGPq zpNGopSJ+ICWuNAoT81+88J34jjTM4Ec0A84saDPuo5R9hJ4Ep9iTzrUhvzKTJ2XO7e0 5S4XRhu878+lOCDX5DsH51/OtHUYl6Y+vgrmY= MIME-Version: 1.0 Received: by 10.236.109.161 with SMTP id s21mr2985177yhg.124.1305412746891; Sat, 14 May 2011 15:39:06 -0700 (PDT) Received: by 10.236.201.65 with HTTP; Sat, 14 May 2011 15:39:06 -0700 (PDT) In-Reply-To: <87y629rl34.fsf@kolob.subpolar.dyndns.org> References: <87y629rl34.fsf@kolob.subpolar.dyndns.org> Date: Sun, 15 May 2011 07:39:06 +0900 Message-ID: Subject: Re: self join From: Ian Lawrence Barwick To: Seb Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.898 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RFC_ABUSE_POST=0.001 X-Spam-Level: X-Archive-Number: 201105/92 X-Sequence-Number: 35376 Hi 2011/5/15 Seb : > Hi, > > This probably reflects my confusion with how self joins work. > > Suppose we have this table: > > =3D# SELECT * FROM tmp; > =C2=A0a | b > ---+--- > =C2=A01 | 2 > =C2=A02 | 3 > =C2=A04 | 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: > > =3D# SELECT * FROM tmp t1, tmp t2 WHERE t2.b <> t1.a; > =C2=A0a | b | a | b > ---+---+---+--- > =C2=A01 | 2 | 1 | 2 > =C2=A01 | 2 | 2 | 3 > =C2=A01 | 2 | 4 | 5 > =C2=A02 | 3 | 2 | 3 > =C2=A02 | 3 | 4 | 5 > =C2=A04 | 5 | 1 | 2 > =C2=A04 | 5 | 2 | 3 > =C2=A04 | 5 | 4 | 5 > (8 rows) > > I need to get: > > =C2=A0a | b | a | b > ---+---+---+--- > =C2=A01 | 2 | 1 | 2 > =C2=A04 | 5 | 4 | 5 > > Or just: > > =C2=A0a | b > ---+--- > =C2=A01 | 2 > =C2=A04 | 5 Your query doesn't have an explicit join and is producing a cartesian resul= t. I don't think a self- join will work here; a subquery should produce the result you're after: SELECT * FROM tmp t1 WHERE NOT EXISTS(SELECT TRUE FROM tmp t2 WHERE t2.b=3Dt1.a); HTH Ian Lawrence Barwick