user2194879
user2194879

Reputation:

PostgreSQL - Joining a table to itself

Does PostgreSQL support self joining or is there another way to solve this?

For example, lets say I have a single table (table a) with the following columns:

id     name     supid
------------------------
1       a         2
2       b         3
3       c         4
4       d         5
..      ..        ..

Is there a way to output the data in the following format?

id      name     sup name
-------------------------
1        a        b
2        b        c
3        c        d
4        d        ..
..       ..       ..

Upvotes: 1

Views: 384

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 181047

How about a simple JOIN?

SELECT a.id,a.name,b.name "sup name"
FROM tablea a
JOIN tablea b
  ON a.supid = b.id

An SQLfiddle to test with.

Upvotes: 3

Related Questions