pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Beutin <psql@laokoon.IN-Berlin.DE>
To: filippo <filippo2991@virgilio.it>
Cc: pgsql-sql@postgresql.org
Subject: Re: join two tables with sharing some columns between two
Date: Mon, 03 Jul 2006 17:54:08 +0200
Message-ID: <44A93DA0.7080201@laokoon.IN-Berlin.DE> (raw)
In-Reply-To: <1151329151.231866.175920@u72g2000cwu.googlegroups.com>
References: <1151329151.231866.175920@u72g2000cwu.googlegroups.com>

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



view thread (3+ messages)

Message-ID: <44A93DA0.7080201@laokoon.IN-Berlin.DE>
Permalink:  ../44A93DA0.7080201@laokoon.IN-Berlin.DE/
Also on:    postgresql.org/message-id/44A93DA0.7080201@laokoon.IN-Berlin.DE

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: psql@laokoon.IN-Berlin.DE, filippo2991@virgilio.it
  Subject: Re: join two tables with sharing some columns between two
  In-Reply-To: <44A93DA0.7080201@laokoon.IN-Berlin.DE>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox