Thread: join two tables with sharing some columns between two

join two tables with sharing some columns between two

From
"filippo"
Date:
Hi,

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).


I is possible to create such a query?

Thanks



Re: join two tables with sharing some columns between two

From
"Phillip Smith"
Date:
If I understand correctly... I think this should work:

SELECT    table1.id,    table1.person_name,    table2.car_description,    table1.date_arrival,    table1.date_departure
FROM    table1, table2
WHERE    table1.id = table2.id
;

Assuming the date_arrival and date_departure fields are the same on both
tables then it doesn't matter if you pull them from table1 or table2,
otherwise you'll need to change the SELECT appropriately.

If you want it to go in to a new table as well as opposed to just a query,
you could have a play with SELECT INTO...

Cheers,
-p

-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of filippo
Sent: Monday, 26 June 2006 23:39
To: pgsql-sql@postgresql.org
Subject: [SQL] join two tables with sharing some columns between two

Hi,

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).


I is possible to create such a query?

Thanks


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate      subscribe-nomail command to
majordomo@postgresql.orgso that your      message can get through to the mailing list cleanly 


*******************Confidentiality and Privilege Notice*******************

The material contained in this message is privileged and confidential to
the addressee.  If you are not the addressee indicated in this message or
responsible for delivery of the message to such person, you may not copy
or deliver this message to anyone, and you should destroy it and kindly
notify the sender by reply email.

Information in this message that does not relate to the official business
of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta.
Weatherbeeta, its employees, contractors or associates shall not be liable
for direct, indirect or consequential loss arising from transmission of this
message or any attachments


Re: join two tables with sharing some columns between two

From
Thomas Beutin
Date:
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).

SELECTid, person_name, NULL AS car_description, date_arrival, date_departureFROM TABLE1
UNION
SELECTid, NULL AS person_name, car_description, date_arrival, date_departureFROM 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