Bluehost.com Web Hosting $6.95

Joining Tables

This is a discussion on Joining Tables within the MySQL Database forums, part of the Database Forums category; Hi I'm a bit stuck for a way to join 4 tables in a single query in MySQL. The ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-03-2007
mou
 
Posts: n/a
Default Joining Tables

Hi

I'm a bit stuck for a way to join 4 tables in a single query in MySQL.

The tables relate to a submission system for users to submit 1 or more
links against a specific topic. The tables are follows:

// table 'topic'
topicID
topicName

// table 'submission'
submissionID
topicID
userID

// table 'link'
linkID
linkURL
submissionID

// table 'user'
userID
userName

I'm trying to get it so when I query it with a specific topic, I want
the details of the topic, submissions and all associated links and
user details. As theres no common table that joins them all, I'm not
sure which table to start the query with and how to get a join to the
other 3.

Any help would be much appreciated.

Reply With Quote
  #2 (permalink)  
Old 06-03-2007
J.O. Aho
 
Posts: n/a
Default Re: Joining Tables

mou wrote:
> Hi
>
> I'm a bit stuck for a way to join 4 tables in a single query in MySQL.
>
> The tables relate to a submission system for users to submit 1 or more
> links against a specific topic. The tables are follows:
>
> // table 'topic'
> topicID
> topicName
>
> // table 'submission'
> submissionID
> topicID
> userID
>
> // table 'link'
> linkID
> linkURL
> submissionID
>
> // table 'user'
> userID
> userName
>
> I'm trying to get it so when I query it with a specific topic, I want
> the details of the topic, submissions and all associated links and
> user details. As theres no common table that joins them all, I'm not
> sure which table to start the query with and how to get a join to the
> other 3.


SELECT tablename.column1, tablename.column2, tablename.column3,
tablename.column4 FROM table1
INNER JOIN table2 ON (table1.joincolumn=table2.joincolumn)
INNER JOIN table3 ON (table2.joincolumn=table3.joincolumn)
INNER JOIN table4 ON (table2.joincolumn=table4.joincolumn)
WHERE tablename.topic='what you are looking for';

Just change the column names and table names and you would have a go, just
replace the "tablename.columnX, ..." to a * if you want all columns.

--

//Aho
Reply With Quote
  #3 (permalink)  
Old 06-04-2007
mou
 
Posts: n/a
Default Re: Joining Tables

On Jun 3, 3:42 pm, "J.O. Aho" <u...@example.net> wrote:
> mou wrote:
> > Hi

>
> > I'm a bit stuck for a way to join 4 tables in a single query in MySQL.

>
> > The tables relate to a submission system for users to submit 1 or more
> > links against a specific topic. The tables are follows:

>
> > // table 'topic'
> > topicID
> > topicName

>
> > // table 'submission'
> > submissionID
> > topicID
> > userID

>
> > // table 'link'
> > linkID
> > linkURL
> > submissionID

>
> > // table 'user'
> > userID
> > userName

>
> > I'm trying to get it so when I query it with a specific topic, I want
> > the details of the topic, submissions and all associated links and
> > user details. As theres no common table that joins them all, I'm not
> > sure which table to start the query with and how to get a join to the
> > other 3.

>
> SELECT tablename.column1, tablename.column2, tablename.column3,
> tablename.column4 FROM table1
> INNER JOIN table2 ON (table1.joincolumn=table2.joincolumn)
> INNER JOIN table3 ON (table2.joincolumn=table3.joincolumn)
> INNER JOIN table4 ON (table2.joincolumn=table4.joincolumn)
> WHERE tablename.topic='what you are looking for';
>
> Just change the column names and table names and you would have a go, just
> replace the "tablename.columnX, ..." to a * if you want all columns.
>
> --
>
> //Aho- Hide quoted text -
>
> - Show quoted text -



Cheers fella, much appreciated.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:36 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0