Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

help with inner join

Explorer ,
Jun 28, 2011 Jun 28, 2011

Copy link to clipboard

Copied

Hello all,

I'm trying to learn something new, and I'm having a little bit of difficulty.  I'm trying to learn about inner joins.  I've looked at countless books, tutorials, and everything else I can find, but I can't seem to figure this out.  Nothing I try works.

I've got tables that are related, and I'm trying to get the data to display.  I've got the database set up as follows:

movies (name of table) has the fields movie_id, movie_name, and movie_star_rating

family_rating (name of table) has the fields star_id, star_name, and star_file_name

in PHPMyAdmin, I've got movies.movie_star_rating internally related to family_rating.star_id

When I display the data, I am trying to pull from the movies table, but instead of having a number displayed, I would like to have family_rating.star_name displayed.  I've got the following code to do this:

$query="SELECT * FROM movies
  INNER JOIN family_rating ON movies.movie_star_rating=family_rating.star_id
  WHERE movies.movie_name_series='$search'";

$result= mysql_query($query)or die (mysql_error());

while($row=mysql_fetch_array($result)){

// Display the data

echo $row['movies.movie_name'];
echo $row['family_rating.star_file_name'];

}


Can anyone let me know what I'm doing wrong?  Thanks for your help.

TOPICS
Server side applications

Views

460
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 28, 2011 Jun 28, 2011

$query="SELECT * FROM movies, family_rating
   INNER JOIN family_rating ON movies.movie_star_rating=family_rating.star_id
   WHERE movies.movie_name_series='$search'";

OK, that SQL is not the same as your original post.

>>Not unique table/alias: 'family_rating'

This tells us where the error is. You are specifying both tables after the from clause. Try it again with your original query:

$query="SELECT * FROM movies
  INNER JOIN family_rating ON movies.movie_star_rating=family_rating.star_id
  WHERE mov

...

Votes

Translate
LEGEND ,
Jun 28, 2011 Jun 28, 2011

Copy link to clipboard

Copied

>I'm trying to learn something new,

>and I'm having a little bit of difficulty.

>I'm trying to learn about inner joins.

Really? Inner joins are just regular joins - the most commonly used join in SQL. Are you thinking about something else?

>Can anyone let me know what I'm doing wrong?

What results are you getting? Show us some sample data from both tables.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2011 Jun 28, 2011

Copy link to clipboard

Copied

Thank you for your reply.  It might be an issue about me thinking about something else, every tutorial and book I've read tells me that what I'm trying to do is an inner join (as opposed to a left join or right join).  But yes, I am trying to get information from another table to display when I query another table.  So, for some sample data:

This is the sample data for the table movies:

movie_idmovie_ratingparent_alertmovie_genre_onemovie_genre_twomovie_genre_threemovie_name
38722272822Test   Movie Entry
movie_name_seriesmovie_star_ratinglinkaliasartdescription
34testmovie.avi2coverart.jpgThis   is a test

This is the sample data for the table family_rating:

star_idstar_namestar_file_name
1
2One   Starone_star.png
3Two   Starstwo_star.png
4Three   Starsthree_star.png
5Four   Starsfour_star.png
11Five   Starsfive_star.png


The results that I'm getting with the code on the web page of:

$query="SELECT * FROM movies, family_rating
  INNER JOIN family_rating ON movies.movie_star_rating=family_rating.star_id
  WHERE movies.movie_name_series='$search'";

$result= mysql_query($query)or die (mysql_error());

while($row=mysql_fetch_array($result)){

// Display the data

echo $row['movies.movie_name'];
echo $row['family_rating.star_file_name'];
}


is:

Not unique table/alias: 'family_rating'

What I'm trying to have as an output is (after a search is performed so the veriable being sent between pages is $search:

Test Movie Entry
One Star
[image of stars]

Obviously I'll ultimately be pulling information from other tables as well, but I'm starting with just two tables so I can learn how this works.  Again, I thank you for your help.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 28, 2011 Jun 28, 2011

Copy link to clipboard

Copied

$query="SELECT * FROM movies, family_rating
   INNER JOIN family_rating ON movies.movie_star_rating=family_rating.star_id
   WHERE movies.movie_name_series='$search'";

OK, that SQL is not the same as your original post.

>>Not unique table/alias: 'family_rating'

This tells us where the error is. You are specifying both tables after the from clause. Try it again with your original query:

$query="SELECT * FROM movies
  INNER JOIN family_rating ON movies.movie_star_rating=family_rating.star_id
  WHERE movies.movie_name_series='$search'";

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 29, 2011 Jun 29, 2011

Copy link to clipboard

Copied

LATEST

Thank you so much for your help.  It worked.  I think I understand it now.  Sorry about pasting a different query from my original post, I was just still playing around with it hoping to get it right.

Again, thank you very much.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines