Jump to content

What is the correct JOIN to use for 3 tables for output


geekisthenewsexy

Recommended Posts

hi guys, i have 3 tables namely course,year and block.

>>in table course, i have 2 fields (c_id, c_name) where c_id is primary and auto-incremented.

>>in table year, i have 3 fields (y_id, c2_id, year) where c2_id is foreign and y_id is unique and auto-incremented.

>>in table block, i have 3 fields also (b_id, y2_id, block) where y2_id is foreign, b_id unique..

now, I'm working on some project that gets input from the user; course,year and block.

 

okay so i tried inputting record couple of times and on the database:

table course:

_______________

c_id | c_name

1    | BEED

2    | BSA

 

table year:

_____________________

y_id | c_id | year

1    | 1    | First year

2    | 1    | Second year

3    | 2    | First year

4    | 2    | Second year

 

table block:

___________________

b_id | y_id | block

1    | 1    | ED1-A

2    | 1    | ED1-B

3    | 2    | ED2-A

4    | 3    | AC1-A

5    | 3    | AC1-B

6    | 4    | AC2-A

 

now, i tried querying like

$result = mysql_query("SELECT DISTINCT c_name,block,year FROM (course LEFT JOIN year ON course.c_id=year.c2_id)RIGHT OUTER JOIN block ON block.y2_id=year.c2_id") or die(mysql_error()); 

(i don't know if this is the right query) but this gives me jumbled output.. :(

 

my problem is what kind of query will i use to be able to make my output like this

_________________________

COURSE | YEAR            | BLOCK

BEED      | First year      | ED1-A

                                    | ED1-B

              | Second year | ED2-A

BSA        | First year      | AC1-A

                                    | AC1-B

              |Second year  | AC2-A

 

..i badly need your help here guys..i'm seriously losing my mind here.. :-\ :'(

                                   

Link to comment
Share on other sites

This might help you in the right direction..

 

SELECT `c_name`, `year`, `block` FROM `course`
Inner Join `year` ON `year`.`c_id` = `course`.`c_id`
Inner Join `block` ON `block`.`y_id` = `year`.`y_id`
ORDER BY `c_name`,`year`

 

It should output something workable within PHP at least *crosses fingers*

Like so.

BEED  First Year   ED1-A

BEED  First Year   ED1-B

BEED  Second Year   ED2-A

BSA    First Year   AC1-A

BSA    First Year   AC1-B

BSA    Second Year   AC2-A

Link to comment
Share on other sites

hi, thanks for the response buddski.  :)

It should output something workable within PHP at least *crosses fingers*

Like so.

so does that mean involving a php code to display

_________________________

COURSE | YEAR            | BLOCK

BEED      | First year      | ED1-A

                                    | ED1-B

              | Second year | ED2-A

BSA        | First year      | AC1-A

                                    | AC1-B

              |Second year  | AC2-A

if what you gave me will work?

Link to comment
Share on other sites

Hi,

 

I spent ages trying to find the correct info for this myself. This is what i worked from and it works. My tables have foreign id's with relationship to primary key.

 

Example:

 

$query="SELECT * FROM table1,table2,table3 WHERE table1.t1_ID=table2.t2_ID AND table2.t2_ID=table3.t3_ID";

 

Hope it helps

Link to comment
Share on other sites

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.