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.
Julie Dowler7,851 Points
SELECT after JOIN?
Link to the challenge: link
Here is the challenge. It's from Integrating PHP with Databases/Filtering Input for Queries/Multiple Conditionals:
Challenge Task 2 of 3
Note: We will be writing ONLY the SQL query for this challenge.
Along with the People table, we also have a Media table with media_id, title, img, format, year and category. To JOIN "many" media items with "many" people, we use a Media_People table which contains a media_id to link to the Media table and a people_id to link to the People table.
Modify your SELECT to pull Media title for all items that are linked to People with the last name "Tolkien"
You can start with the following code from the last task:
SELECT * FROM People WHERE fullname LIKE '%Tolkien';
So, here is my query:
SELECT * FROM People WHERE fullname LIKE '%Tolkien'
JOIN Media_People ON People.people_id = Media_People.people_id
JOIN Media ON Media_People.media_id = Media.media_id
SELECT title FROM Media
Tom Gooding16,735 Points
You SELECT the column as you would usually before the JOIN. However you need to specify the table as well. Here for example you need to SELECT Media.title FROM ...
SELECT Media.title FROM People INNER JOIN Media_people ON People.people_id = Media_People.people_id INNER JOIN Media ON Media_People.media_id = Media.media_id WHERE fullname LIKE '%Tolkien';