Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

SQL excersice bummering.... You need to specify tables ON the JOIN

Excersice: We will be writing ONLY the SQL query for this challenge.

The library database contains a Media table with the columns media_id, title, img, format, year and category. It also contains a Genres table with the columns genre_id and genre. To join these tables, there is a Media_Genres table that contains the column media_id and genre_id

Add to the following SELECT statement to JOIN the Media table and the Genres table using the joining table Media_Genres.

NOTE: You will need to add the table to the WHERE clause so that the media_id column is not ambiguous.


Since table Media has a genre_id column (which by the way is not mentioned in the description) so

SELECT * FROM Media JOIN Genres ON Media.genre_id = Genres.genre_id WHERE Media.media_id = 1

should do the job, but that is not the exercise.

so is should JOIN Media table with Media_Genres table ON the media_id to get the Media_Genres.genre_id to then JOIN Genres table on genre_id so we can retrieve the genre.

I'm getting nowhere after +/- 30 minutes of tying several possibilities i only get errors.

Select * From Media JOIN Media_Genres ON Media_Genres.genre_id = Media.genre_id JOIN Genres ON Genres.genre_id = Media.genre_id Where Media.media_id = 1 bummer: You need to specify tables ON the JOIN or

Select * From Media JOIN Media_Genres ON Media_Genres.genre_id = Media.genre_id JOIN Genres ON Genres.media_id = Media.media_id Where Media.media_id = 1 bummer: You need to specify tables ON the JOIN

4 Answers

OK, not sure where you got the WHERE Media.media_id = 1 but this is what is needed instead:

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

In the SQL DBMSs I've worked with the final WHERE clause would be redundant, so I'm not sure why they require it. But anyway, this version works!

Ok, that worked and i understand where i went wrong. Thanks for the help.

Harm, can you give me a link to the challenge?

Select * From Media 
JOIN Media_Genres ON Media_Genres.genre_id = Media.genre_id 
JOIN Genres ON Genres.media_id = Media.media_id 
WHERE Media.media_id = 1

The SQL looks OK, so there must be something else going on here.

Gerry Tarney
Gerry Tarney
3,815 Points

Having a similar problem. Continuing to get an ambiguous error even when specifying the table.

SELECT * FROM Media JOIN Media_Genres On Media_Genres.genre_id = Media.genre_id LEFT OUTER JOIN Media ON Media.genre_id = Genres.genre_id WHERE Media.media_id = 3;

SQL Error: ambiguous column name: main.Media.media_id

https://teamtreehouse.com/library/integrating-php-with-databases/using-relational-tables/joining-tables