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

Databases

Anthony Bilotti
Anthony Bilotti
4,332 Points

SQL Error: Ambiguous Column Name: media_id

I was trying to join two tables together when I got the error SQL Error: Ambiguous Column Name: media_id. I do not know what happened! Please help me!

Code Attached

Anthony, you say "Code Attached" but I don't see any.

2 Answers

Steven Parker
Steven Parker
229,788 Points

That error probably indicates that both tables have a column named media_id. Just put the table name that you want to select the column from and a period in front of the column name.

For example, if the table name was foo, you might have a query that starts like this:

SELECT foo.media_id ...
Jessica Murillo
Jessica Murillo
9,119 Points

I had the same problem. Steven Parker was right. Since there are two tables with the column name media_id it does know what table to pull that column from so you have to add Media before media_id in the SELECT statement. It worked for me after adding it.

SELECT Media.media_id, title, category, img, format, year, genre, publisher, isbn 
            FROM Media
            JOIN Genres ON Media.genre_id = Genres.genre_id
            LEFT OUTER JOIN Books ON Media.media_id = Books.media_id 
            WHERE Media.media_id = $id;
Jessica Murillo
Jessica Murillo
9,119 Points

I know this was a few months ago too but I am sure that other people will have this same problem.