X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 60CFA9DCBDB for ; Fri, 10 Mar 2006 13:30:26 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 11198-08 for ; Fri, 10 Mar 2006 13:30:25 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from wolff.to (wolff.to [66.93.197.194]) by postgresql.org (Postfix) with SMTP id BDEDD9DC86D for ; Fri, 10 Mar 2006 13:30:23 -0400 (AST) Received: (qmail 9503 invoked by uid 500); 10 Mar 2006 17:31:13 -0000 Date: Fri, 10 Mar 2006 11:31:13 -0600 From: Bruno Wolff III To: Fay Du Cc: pgsql-sql@postgresql.org Subject: Re: How can I selet rows which have 2 columns values cross equal? Message-ID: <20060310173113.GA8935@wolff.to> Mail-Followup-To: Bruno Wolff III , Fay Du , pgsql-sql@postgresql.org References: <001d01c64445$fec23350$b8d849c6@versaterm.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <001d01c64445$fec23350$b8d849c6@versaterm.com> User-Agent: Mutt/1.4.2.1i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.101 required=5 tests=[AWL=0.101] X-Spam-Score: 0.101 X-Spam-Level: X-Archive-Number: 200603/151 X-Sequence-Number: 24497 On Fri, Mar 10, 2006 at 08:24:44 -0500, Fay Du wrote: > Hi All: > I have a table like this: > > Table test > Id | a | b > -----+----+--- > 1 | 100| 101 > 2 | 101| 100 > 3 | 100| 3 > 4 | 20 | 30 > 5 | 11 | 13 > 6 | 3 | 33 > 7 | 30 | 20 > > I want to get row 1, 2,4 and 7 selected. Because their values of a and b > are cross equal. i.e., for each pair of rows, > a.Row1 = b.Row2 and b.Ro1 = a.Row2 > a.Row4 = b.Row7 and b.Ro4 = a.Row7 > > How can I construct a sql statement to select them? > Thanks in advance. Self join the table. Something like (untested): SELECT t1.id, t1.a, t1.b FROM test t1, test t2 WHERE t1.a = t2.b AND t1.b = t2.a ORDER BY t1.id ;