Unix Technical Forum

Querying from two tables as if they were appended

This is a discussion on Querying from two tables as if they were appended within the pgsql Novice forums, part of the PostgreSQL category; --> I have two tables with an identical layout. The difference is that one has data from this year and ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 09:52 PM
David Gaudine
 
Posts: n/a
Default Querying from two tables as if they were appended

I have two tables with an identical layout. The difference is that one
has data from this year and one has data from last year. For a
simplified example let's call them "table05" and "table06", and say that
each has a single column called "name". So we have

table05:
David
Bob

table06:
David
John
Paul

I want to select all rows from both tables, so the result (order not
important) is
David
Bob
David
John
Paul

This is surely trivial, but I read the section about joined tables and
it shows only how to give a result that has 2 columns, like

test=# select * from table05,table06;
name | name
-------+-------
David | David
David | John
David | Paul
Bob | David
Bob | John
Bob | Paul

One solution would to to create a third table and copy the first two
into it, but since the two tables change frequently this is
undesirable. How can I do it by querying the existing tables?

Note: This example is oversimplified. The real query will also test
other columns, like for example
select * from ( however I do the above) where age > 25
so the number of rows in the result will not be the sum of the number of
rows in each table.

David


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 09:52 PM
A. Kretschmer
 
Posts: n/a
Default Re: Querying from two tables as if they were appended

am 17.05.2006, um 11:43:59 -0400 mailte David Gaudine folgendes:
> I have two tables with an identical layout. The difference is that one has
> data from this year and one has data from last year. For a simplified
> example let's call them "table05" and "table06", and say that each has a
> single column called "name". So we have
>
> table05:
> David
> Bob
>
> table06:
> David
> John
> Paul
>
> I want to select all rows from both tables, so the result (order not
> important) is
> David
> Bob
> David
> John
> Paul


select col from table05 union all select col from table06;




HTH, Andreas.
--
Andreas Kretschmer (Kontakt: siehe Header)
Heynitz: 035242/47215, D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
=== Schollglas Unternehmensgruppe ===

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:22 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com