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 B0CDC9DC80C for ; Sat, 11 Mar 2006 03:46:35 -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 76665-07 for ; Sat, 11 Mar 2006 03:46:35 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtp-send.myrealbox.com (smtp-send.myrealbox.com [151.155.5.143]) by postgresql.org (Postfix) with ESMTP id F2E379DCB7C for ; Sat, 11 Mar 2006 03:46:32 -0400 (AST) Received: from [10.10.14.68] grzm [203.178.83.78] by smtp-send.myrealbox.com with NetMail SMTP Agent $Revision: 1.6 $ on Linux; Sat, 11 Mar 2006 00:46:33 -0700 In-Reply-To: <001d01c64445$fec23350$b8d849c6@versaterm.com> References: <001d01c64445$fec23350$b8d849c6@versaterm.com> Mime-Version: 1.0 (Apple Message framework v746.2) X-Priority: 3 (Normal) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: How can I selet rows which have 2 columns values cross equal? Date: Sat, 11 Mar 2006 16:46:29 +0900 To: "Fay Du" X-Mailer: Apple Mail (2.746.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.104 required=5 tests=[AWL=-0.228, RCVD_IN_BL_SPAMCOP_NET=1.332] X-Spam-Score: 1.104 X-Spam-Level: * X-Archive-Number: 200603/158 X-Sequence-Number: 24504 On Mar 10, 2006, at 22:24 , Fay Du wrote: > 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 You need to use subqueries: create table test ( id integer primary key , a integer not null , b integer not null ); copy test (id, a, b) from stdin; 1 100 101 2 101 100 3 100 3 4 20 30 5 11 13 6 3 33 7 30 20 \. select t1.id as t1_id, t2.id as t2_id from test t1 join test t2 on (t1.a = t2.b and t1.b = t2.a); t1_id | t2_id -------+------- 7 | 4 4 | 7 2 | 1 1 | 2 (4 rows) And if you don't want to have each pair listed twice, just add WHERE t1.a < t2.a, e.g., select t1.id as t1_id, t2.id as t2_id from test t1 join test t2 on (t1.a = t2.b and t1.b = t2.a) where t1.a < t2.a; t1_id | t2_id -------+------- 4 | 7 1 | 2 (2 rows) Hope this helps! Michael Glaesemann grzm myrealbox com