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 trialJohn Lukacs
26,806 PointsWe 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
Front End Web Development Techdegree Graduate 32,425 PointsHi 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
Front End Web Development Techdegree Graduate 32,425 PointsHi 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
26,806 PointsDon't you have to use the Join and where key words thanks
John Lukacs
26,806 PointsOh you did use it Im stupid
John Lukacs
26,806 PointsSELECT 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
26,806 Pointsthank 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
Front End Web Development Techdegree Graduate 32,425 PointsThe = 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:-)