Jump to content

Recommended Posts

Good Day,

 

I'm trying to JOIN 3 tables with a query, I can get it to work fine with 2, but having no luck with 3. Firstly here is my table names and their Primary/Foreign Keys.

 

Table: categories

Primary Key: cat_id

 

Table: forums

Primary Key: forum_id

Foreign Key: cat_id

 

Table: threads

Primary Key: thread_id

Foreign Key: forum_id

 

My current query SELECT's fields from `categories` and `forums` JOIN'ing them with `cat_id` and this works fine:

SELECT `categories`.`cat_id`, `categories`.`cat_name`, `forums`.`forum_name`

                                        FROM `categories`

                                        INNER JOIN `forums`

                                        USING (`cat_id`)

                                        ORDER BY `categories`.`cat_order` ASC

But I also want to SELECT `threads`.`thread_id` which needs to be JOIN'ed by... `threads`.`forum_id` = `forums`.`forum_id` somehow, I've tried several different combinations and can't get it to work.

 

Thanks for any help!

 

Cheers,

Ace

 

SELECT `categories`.`cat_id`, `categories`.`cat_name`, `forums`.`forum_name`, `threads`.`thread_id`

                                        FROM `categories`, `forums`, `threads`

                                        WHERE `categories`.`cat_id` = `forums`.`cat_id`

                                        AND `forums`.`forum_id` = `threads`.`forum_id`

                                        ORDER BY `categories`.`cat_order` ASC

Returns zero results.

have you tried:

SELECT `categories`.`cat_id`, `categories`.`cat_name`, `forums`.`forum_name`, `threads`.`thread_id`
FROM `categories`
INNER JOIN `forums`ON (categories.cat_id = forums.cat_id)
LEFT JOIN `threads` ON (threads.forum_id = forums.forum_id)

ORDER BY `categories`.`cat_order` ASC

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.