Jump to content

many-to-many link table problem


Maracles

Recommended Posts

Hi,

 

I am trying to design my first link table and struggling despite reading this tutorial:

 

http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html#03

 

I am working on a movie-based site within which each film has its own page. On a page for a specific film there will be a 'Cast' section in which I want to list all actors from the movie.

 

I have three tables;

 

'title' for the film details. Each film has a unique filmID

'talent' for the cast details. Each actor has a unique talentID

'film2talent' which I aimed to be the link table.

 

Can someone advise what mysql query I would need to link them together? I do not understand how the link table is populated (i.e. do I populate it whenever I add actors or movies to my databae or do I only populate it when I am running a query?)

 

I guess the logic would be:

 

Look in link table for filmID, select all talentIDs that correspond to that filmID and then select all talent information relating to the talentIDs.

 

How do I do this in mysql code though? Any help is MUCH appreciated!

Link to comment
Share on other sites

You would populate the link table when adding a new actor or movie. So if Chrstian Bale is in like 10 movies, when he is added you would need to add all 10 elements into the link table.

 

To grab what you would need, your query would look similar to this...

 

SELECT m.`film`, a.`name` FROM `title` AS `m` LEFT JOIN `film2talent` AS `l` ON m.`id`=l.`film` LEFT JOIN `talent` AS `a` ON l.`talent`=a.`id`

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.