简体   繁体   English

MySQL和PHP - 不是唯一的表/别名

[英]MySQL & PHP - Not unique table/alias

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;
}
}

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. 你可能想加入articles_grades。

You need to use an alias if you're using the same name twice: 如果您使用两次相同的名称,则需要使用别名:

SELECT FROM grades g1 ... 
JOIN grades g2 ON g1.id = g2.grade_id ...

Be sure that you intended to use the same name twice, and didn't mistakenly enter the same name twice. 确保您打算两次使用相同的名称,并且没有错误地两次输入相同的名称。

它说是因为你在查询中有两次表名等级

I thin that in the $sql2 query the second table isn't grades but article_grades. 我在$ sql2查询中瘦了第二个表不是成绩而是article_grades。 so it will be: 所以它将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM