Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 568F3634DC2 for ; Fri, 22 May 2009 11:09:09 -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 33654-01-2 for ; Fri, 22 May 2009 11:09:07 -0300 (ADT) X-Greylist: delayed 00:19:22.025777 by SQLgrey-1.7.6 Received: from mail.cs.umu.se (mail.cs.umu.se [130.239.40.25]) by mail.postgresql.org (Postfix) with ESMTP id DC1E1633050 for ; Fri, 22 May 2009 11:09:02 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by amavisd-new (Postfix) with ESMTP id 9211A81 for ; Fri, 22 May 2009 15:49:37 +0200 (MEST) X-Virus-Scanned: amavisd-new at cs.umu.se Received: from mail.cs.umu.se ([127.0.0.1]) by localhost (mimmi.cs.umu.se [127.0.0.1]) (amavisd-new, port 10024) with LMTP id yGaMoP+U5Pkv for ; Fri, 22 May 2009 15:49:34 +0200 (MEST) Received: from [192.168.0.3] (213-21-81-232.bon.t3.se [213.21.81.232]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.cs.umu.se (Postfix) with ESMTPS id 68C0C84 for ; Fri, 22 May 2009 15:49:34 +0200 (MEST) Message-ID: <4A16AD3B.5040407@cs.umu.se> Date: Fri, 22 May 2009 15:48:43 +0200 From: Richard Ekblom User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Need help combining 2 tables together Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200905/93 X-Sequence-Number: 32585 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