Re: How can I selet rows which have 2 columns values cross equal?

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Fay Du <fay(dot)du(at)versaterm(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: How can I selet rows which have 2 columns values cross equal?
Date: 2006-03-10 17:31:13
Message-ID: 20060310173113.GA8935@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, Mar 10, 2006 at 08:24:44 -0500,
Fay Du <fay(dot)du(at)versaterm(dot)com> 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
;

In response to

Responses

  • Locking row at 2006-03-10 20:50:10 from Flavio Suguimoto

Browse pgsql-sql by date

  From Date Subject
Next Message Flavio Suguimoto 2006-03-10 20:50:10 Locking row
Previous Message Tom Lane 2006-03-10 14:48:48 Re: Set generating functions and subqueries