Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL & PHP - Not unique table/alias

Tags:

I get the following error listed below and was wondering how do I fix this problem.

Not unique table/alias: 'grades' 

Here is the code I think is giving me the problem.

function getRating(){ $dbc = mysqli_connect ("localhost", "root", "", "sitename");  $page = '3';  $sql1 = "SELECT COUNT(*)           FROM articles_grades           WHERE users_articles_id = '$page'";  $result = mysqli_query($dbc,$sql1);  if (!mysqli_query($dbc, $sql1)) {         print mysqli_error($dbc);         return; }  $total_ratings = mysqli_fetch_array($result);  $sql2 = "SELECT COUNT(*)           FROM grades           JOIN grades ON grades.id = articles_grades.grade_id          WHERE articles_grades.users_articles_id = '$page'";  $result = mysqli_query($dbc,$sql2);  if (!mysqli_query($dbc, $sql2)) {         print mysqli_error($dbc);         return; }  $total_rating_points = mysqli_fetch_array($result); if(!empty($total_rating_points) && !empty($total_ratings)){ // set the width of star for the star rating $rating = (round($total_rating_points / $total_ratings,1)) * 10;  echo $rating; } else {     $rating = 100;      echo $rating; } } 
like image 768
tEcHnUt Avatar asked Jan 16 '10 12:01

tEcHnUt


1 Answers

The problem seems to be here:

SELECT COUNT(*)  FROM grades  JOIN grades ON grades.id = articles_grades.grade_id WHERE articles_grades.users_articles_id = '$page'" 

You are trying to join the table grades to itself. You probably meant to join with articles_grades.

like image 96
Mark Byers Avatar answered Sep 20 '22 12:09

Mark Byers