self join - Mailing list pgsql-sql

From Seb
Subject self join
Date
Msg-id 87y629rl34.fsf@kolob.subpolar.dyndns.org
Whole thread Raw
Responses Re: self join  (Ian Lawrence Barwick <barwick@gmail.com>)
List pgsql-sql
Hi,

This probably reflects my confusion with how self joins work.

Suppose we have this table:

=# SELECT * FROM tmp;a | b 
---+---1 | 22 | 34 | 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:

=# SELECT * FROM tmp t1, tmp t2 WHERE t2.b <> t1.a;a | b | a | b 
---+---+---+---1 | 2 | 1 | 21 | 2 | 2 | 31 | 2 | 4 | 52 | 3 | 2 | 32 | 3 | 4 | 54 | 5 | 1 | 24 | 5 | 2 | 34 | 5 | 4 |
5
(8 rows)

I need to get:
a | b | a | b 
---+---+---+---1 | 2 | 1 | 24 | 5 | 4 | 5

Or just:
a | b  
---+---1 | 2 4 | 5 


-- 
Seb



pgsql-sql by date:

Previous
From: Charlie
Date:
Subject: Re: [SQL] Sorting data based fields in two linked tables
Next
From: Ian Lawrence Barwick
Date:
Subject: Re: self join