Re: join two tables with sharing some columns between two

From: Thomas Beutin <psql(at)laokoon(dot)IN-Berlin(dot)DE>
To: filippo <filippo2991(at)virgilio(dot)it>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: join two tables with sharing some columns between two
Date: 2006-07-03 15:54:08
Message-ID: 44A93DA0.7080201@laokoon.IN-Berlin.DE
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

filippo wrote:
> I have two tables like these: (this is an example, the actual tables
> have diffferent fields and meanings)
>
> TABLE1
> id
> person_name
> date_arrival
> date_departure
>
> TABLE2
> id
> car_description
> date_arrival
> date_departure
>
> I'd like to make a query to have such resulting table
>
> RESULTING_TABLE
> id
> person_name
> car_description
> date_arrival
> date_departure
>
> the id is the primary key for the three tables and it's unique for the
> three (id in table1 cannot be in table2, I use the same counter to
> generate the id for table1 and table2).

SELECT
id, person_name, NULL AS car_description, date_arrival, date_departure
FROM TABLE1
UNION
SELECT
id, NULL AS person_name, car_description, date_arrival, date_departure
FROM TABLE2
ORDER BY <what you want>

But be careful, w/o constraints there's no guarantee that the id's are
really unique on both tables!

HTH,
-tb

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bruno Wolff III 2006-07-03 16:06:43 Re: unique values of profile in the whole system
Previous Message Tom Lane 2006-07-03 15:37:54 Re: Local variable and column name conflict