Joins within a table

From: Steve Lefevre <lefevre(dot)10(at)osu(dot)edu>
To: pgsql-novice(at)postgresql(dot)org
Subject: Joins within a table
Date: 2007-06-12 22:59:43
Message-ID: 466F255F.4020204@osu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello all -

I'm trying to do a JOIN within a table. In MySQL, I would do

SELECT main_table.field, join_table.field
FROM main_table
LEFT JOIN main_table AS join_table ON join_table.id = main_table.parent_id

Or something like that.

However, in the Postgres documentation, I don't see anything on JOIN AS.
How would I do a join within a table in Postgres?

The table in question is a genetics table of viruses. The columns are
id, strain, and parent_id. Since viruses only replicate, each strain has
one parent. This way I can have one table and show entire tree of the
relationship. It guarantees that I have only one parent per virus.

The only other way I can think of doing it would be to have two tables:

table strains
------------
id
strain

table relationships
--------------
parent_id
child_id

But then I think I would have to implement some constraints so that I
don't get more than one parent per child.

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Richard Broersma Jr 2007-06-12 23:23:42 Re: Joins within a table
Previous Message Steve Lefevre 2007-06-12 22:49:37 no results for nextval() query?