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 1AFF19DC989 for ; Sat, 11 Mar 2006 05:35:08 -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 07176-02 for ; Sat, 11 Mar 2006 05:35:09 -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 1BF529DC9B0 for ; Sat, 11 Mar 2006 05:35:06 -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 02:35:08 -0700 In-Reply-To: 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: <38FFEAFB-E746-408A-AB17-6A14FAABF731@myrealbox.com> Cc: "Fay Du" , 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 18:35:04 +0900 To: Michael Glaesemann X-Mailer: Apple Mail (2.746.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.44 required=5 tests=[AWL=0.440] X-Spam-Score: 0.44 X-Spam-Level: X-Archive-Number: 200603/159 X-Sequence-Number: 24505 On Mar 11, 2006, at 16:46 , Michael Glaesemann wrote: > 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) Just a follow-up (mostly to myself): I've been toying with using natural joins recently, and here's the same query rewritten to use a natural join: select id as t1_id, t2_id from test t1 natural join ( select id as t2_id , a as b , b as a from test ) t2 where id < t2_id; t1_id | t2_id -------+------- 4 | 7 1 | 2 (2 rows) Michael Glaesemann grzm myrealbox com