Jump to content

JOIN and use unique data from same column names


SF23103

Recommended Posts

I have data similar to below.  I am doing a MySQL query and doing a JOIN based on instructor name.  I am echoing classes and linking the instructor name to another page with their BIO.  All is working great, however I want to use the ID's for something else.  How do I differentiate the ID between the Instructors table and the Classes Table without changing the column name in the database?  

 

For instance, I want to echo the Instructor ID and then echo the Classes ID, but they are both the same column name in the database and since they are joined, how will that work?

 

 

Instructors:

 

ID  |  Instructor    |  Title         |  Bio                                          |

1    |  Bob Smith  |  Chef       |  bob smith is a chef with...        |

2   |  Jane Doe    | Professor |  Jane doe is an instructor that... |

 

Classes:

 

ID  |  Instructor   |  Class Title      |  Class Description                                          |

4    |  Bob Smith  |  Baking      | this is a baking class where you learn....        |

5   |  Jane Doe    | Food Safety |  Food safety is very important... |

Link to comment
Share on other sites

Use column aliases.

SELECT i.ID as instructorID
  , c.ID as classID
  , i.instructor
  , `class title` 
FROM instructor i
    INNER JOIN classes c USING (Instructor)

Don't uses column names containing spaces.

 

The classes table should contain the instructor id (as a foreign key) and not the name. The name should only appear in the instructor table.

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.