pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <aklaver@comcast.net>
To: pgsql-sql@postgresql.org
Cc: Richard Ekblom <ens01rem@cs.umu.se>
Subject: Re: Need help combining 2 tables together
Date: Fri, 22 May 2009 07:17:46 -0700
Message-ID: <200905220717.46444.aklaver@comcast.net> (raw)
In-Reply-To: <4A16AD3B.5040407@cs.umu.se>
References: <4A16AD3B.5040407@cs.umu.se>

On Friday 22 May 2009 6:48:43 am Richard Ekblom wrote:
> Hello
>
> I have frequently encountered the need of combining two tables into one.
> First, please take a look at the following table setups...
>
> CREATE TABLE topics (
>    id SERIAL PRIMARY KEY,
>    topic TEXT NOT NULL
> );
>
> CREATE TABLE messages (
>    id SERIAL PRIMARY KEY,
>    topic INTEGER REFERENCES topics(id),
>    message TEXT NOT NULL
> );
>
> Example of a topics table:
> ID    TOPIC
> 1     Are squares better then circles?
> 2     My favorite food
>
> Example of a messages table:
> ID    TOPIC    MESSAGE
> 1     2        I like lasagna!
> 2     2        Pizza is also a favorite
> 3     1        I like circles, they remind me of pizza
>
> Notice that the number of topics may differ from the number of messages.
>
> Now I want to combine these tables with a single SELECT to get...
>
> Combined table:
> ID   TOPIC                               MESSAGE
> 1    My favorite food                    I like lasagna!
> 2    My favorite food                    Pizza is also a favorite
> 3    Are squares better then circles?    I like circles, they remind me
> of pizza
>
> I have seen different examples of this with something called JOIN but
> they always give me only two rows. How can I do this when the two tables
> may have different sizes to produce exactly the combined table above???
>
>
> Some SQL for Postgres if you want to set up this example...
>
> CREATE TABLE topics (id SERIAL PRIMARY KEY,topic TEXT NOT NULL);
> CREATE TABLE messages (id SERIAL PRIMARY KEY,topic INTEGER REFERENCES
> topics(id),message TEXT NOT NULL);
> INSERT INTO topics(topic) VALUES('Are squares better then circles?');
> INSERT INTO topics(topic) VALUES('My favorite food');
> INSERT INTO messages(topic,message) VALUES(2, 'I like lasagna!');
> INSERT INTO messages(topic,message) VALUES(2, 'Pizza is also a favorite');
> INSERT INTO messages(topic,message) VALUES(1, 'I like circles, they
> remind me of pizza');
> SELECT * FROM topics;
> SELECT * FROM messages;
>
>
> Thanks in advance
> /RE

test=# SELECT m.id,t.topic,m.message from topics as t,messages as m where 
m.topic=t.id order by m.id;
 id |              topic               |         message
----+----------------------------------+--------------------------
  1 | My favorite food                 | I like lasagna!
  2 | My favorite food                 | Pizza is also a favorite
  3 | Are squares better then circles? | I like circles, they
                                       : remind me of pizza


-- 
Adrian Klaver
aklaver@comcast.net



view thread (5+ messages)  latest in thread

Message-ID: <200905220717.46444.aklaver@comcast.net>
Permalink:  ../200905220717.46444.aklaver@comcast.net/
Also on:    postgresql.org/message-id/200905220717.46444.aklaver@comcast.net

 · 

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: aklaver@comcast.net, ens01rem@cs.umu.se
  Subject: Re: Need help combining 2 tables together
  In-Reply-To: <200905220717.46444.aklaver@comcast.net>

* 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