Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 579D06327A7 for ; Fri, 22 May 2009 11:17:50 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 35491-07 for ; Fri, 22 May 2009 11:17:48 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from QMTA05.emeryville.ca.mail.comcast.net (qmta05.emeryville.ca.mail.comcast.net [76.96.30.48]) by mail.postgresql.org (Postfix) with ESMTP id EEB886321E2 for ; Fri, 22 May 2009 11:17:48 -0300 (ADT) Received: from OMTA14.emeryville.ca.mail.comcast.net ([76.96.30.60]) by QMTA05.emeryville.ca.mail.comcast.net with comcast id ucN21b0051HpZEsA5eHoHs; Fri, 22 May 2009 14:17:48 +0000 Received: from tucker.local ([24.17.164.54]) by OMTA14.emeryville.ca.mail.comcast.net with comcast id ueHm1b00L1Ak4z48aeHnsP; Fri, 22 May 2009 14:17:47 +0000 From: Adrian Klaver To: pgsql-sql@postgresql.org Subject: Re: Need help combining 2 tables together Date: Fri, 22 May 2009 07:17:46 -0700 User-Agent: KMail/1.9.10 Cc: Richard Ekblom References: <4A16AD3B.5040407@cs.umu.se> In-Reply-To: <4A16AD3B.5040407@cs.umu.se> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200905220717.46444.aklaver@comcast.net> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200905/94 X-Sequence-Number: 32586 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