Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

John Lukacs
John Lukacs
26,806 Points

We will be writing ONLY the SQL query for this challenge. The library database contains a Media table with the column

Can somebody tell me how to Join two table to table where the Where clause is used.

Can you please go threw the challenge solve it and explain it.

7 Answers

Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

Hi John,

This is a tough one.....

This is what I had initially come up with, but it did not work:

SELECT * FROM Media, Genres JOIN Media_Genres ON Media.media_id AND Genres.genre_id WHERE Media.media_id AND Genres.genre_id=3; 

Then I tried this, which also did not work:

SELECT * FROM Media JOIN Media_Genres ON Media.media_id = Media_Genres.media_id JOIN Genres ON Media_genres.genre_id = Genres.genre_id WHERE Media_Genres.media_id = 3;

..and then finally arrived at the solution:

SELECT * FROM Media 
JOIN Media_Genres ON Media.media_id = Media_Genres.media_id 
JOIN Genres ON Genres.genre_id = Media_Genres.genre_id
WHERE Media_Genres.media_id = Media.media_id
Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

Hi John,

Can you provide a link to the challenge that you are having problems with?

In the meantime, here is an example of joining two tables together with a WHERE clause:

SELECT e.first_name, e.last_name, u.user_type, u.username FROM `employee` AS e INNER JOIN `user` AS u ON e.id = u.employee_id WHERE e.salary > 6000 AND u.user_type = 'ADMIN';

http://www.mysqltutorial.org/mysql-inner-join.aspx

Hope that helps:-)

John Lukacs
John Lukacs
26,806 Points

Oh you did use it Im stupid

John Lukacs
John Lukacs
26,806 Points
SELECT media_id, title, img, format, year, category FROM Media WHERE media_id = Media INNER JOIN Genres ON join_condition1
INNER JOIN Media_Genres ON join_condition2;

Im not sure how to use a WHERE clause to make the first column not ambigous also don't understand what it is asking for in the join_condition statement1 or join_condition2

John Lukacs
John Lukacs
26,806 Points

thank you Could you explain I don't understand The where clause why is WHERE Media_Genres.media_id = Media.media_id

And why is there a = in the join clauses

Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

The = sign is there to join the media_id from the media table with the media_id from the Media_Genres table (in other words to say that they are one in the same) and like wise with the the genre_id from the Genres table as it is related to the genre_id in the Media_genre table. That's how I understand it.

As for the WHERE clause, I'm not certain as to why it needs to be there as we have already assigned the ids from the tables to their associated ids. I would imagine that we could have also stated the same for the Genres.genre_id as well. Maybe someone could explain this part a bit better than me.

I've now just signed up for this class as I'm a bit rusty with MySQL....need to refresh.

Hope this helps:-)