为什么以下查询返回所有记录而不是一个?

时间:2016-04-20 11:48:43

标签: mysql

我在下面写了一个查询来从DB中获取与id值匹配的记录。但是此查询返回所有记录,而不是仅返回一个id = 1的记录。

$movie_id=$_GET['id'];   //assume movie_id=1
//$sql ='select * from tbl_movie where movie_id='.$_GET["id"];

$sql='SELECT M.movie_name, MC.on_screen_name, R.role, C.celebrity_name, C.celebrity_id, MI.production, MI.director,'
        . ' MI.screenplay, MI.music, MI.bgm_score, Col.movie_running_time, L.language, CC.censor_certificate,'
        . ' MR.movie_review_comment, A.award_name'
        . ' FROM tbl_movie M INNER JOIN tbl_movie_awards MA INNER JOIN tbl_movie_details MD'
        . ' INNER JOIN tbl_movie_cast MC on MC.movie_id='.$movie_id
        . ' INNER JOIN tbl_actor_role R on MC.movie_role_id=R.role_id AND MC.movie_id='.$movie_id
        . ' INNER JOIN tbl_celebrity C on MC.movie_celebrity_id=C.celebrity_id AND MC.movie_id='.$movie_id
        . ' INNER JOIN tbl_awards A on MA.award_id=A.award_id AND MA.award_movie_id='.$movie_id
        . ' INNER JOIN tbl_language L on MD.movie_language_id=L.language_id'
        . ' INNER JOIN tbl_censor_certificate CC on MD.censor_id=CC.censor_id AND MD.movie_id='.$movie_id
        . ' INNER JOIN tbl_movie_info MI on MI.movie_info_id='.$movie_id
        . ' INNER JOIN tbl_movie_collection Col on Col.movie_id='.$movie_id
        . ' INNER JOIN tbl_tt_movie_review MR on MR.movie_review_id='.$movie_id.';';

$result = $db->getData($sql);
    if(!empty($result))
    {
        while($row=$result->fetch_assoc())
        {
             $movie_name=$row['movie_name'];
        }

    }

getData函数写在另一个文件中:

public function getData($query)
{
    $result=$this->myconn->query($query);
    if($result->num_rows>0)
    {
        return $result;
    }
    else
    {
        echo ' error in query execution'.$this->myconn->error;
    }
}

我不知道上述查询中的问题是什么。任何人都可以建议如何得到正确答案吗?

1 个答案:

答案 0 :(得分:1)

假设顶部的注释行(// $ sql)运行良好,请尝试:

$sql='SELECT M.movie_name, MC.on_screen_name, R.role, C.celebrity_name, C.celebrity_id, MI.production, MI.director,'
        . ' MI.screenplay, MI.music, MI.bgm_score, Col.movie_running_time, L.language, CC.censor_certificate,'
        . ' MR.movie_review_comment, A.award_name'
        . ' FROM tbl_movie M INNER JOIN tbl_movie_awards MA INNER JOIN tbl_movie_details MD'
        . ' INNER JOIN tbl_movie_cast MC on MC.movie_id='.$movie_id
        . ' INNER JOIN tbl_actor_role R on MC.movie_role_id=R.role_id AND MC.movie_id='.$movie_id
        . ' INNER JOIN tbl_celebrity C on MC.movie_celebrity_id=C.celebrity_id AND MC.movie_id='.$movie_id
        . ' INNER JOIN tbl_awards A on MA.award_id=A.award_id AND MA.award_movie_id='.$movie_id
        . ' INNER JOIN tbl_language L on MD.movie_language_id=L.language_id'
        . ' INNER JOIN tbl_censor_certificate CC on MD.censor_id=CC.censor_id AND MD.movie_id='.$movie_id
        . ' INNER JOIN tbl_movie_info MI on MI.movie_info_id='.$movie_id
        . ' INNER JOIN tbl_movie_collection Col on Col.movie_id='.$movie_id
        . ' INNER JOIN tbl_tt_movie_review MR on MR.movie_review_id='.$movie_id
        . ' WHERE M.movie_id = '.$movie_id.';';

您正在过滤与电影相关的表格(INNER JOIN ... ON id = $ movie_id),但WHERE语句会过滤检索到的结果。

编辑:实际上,我正在检查你的查询,INNER JOIN之后的ON子句是告诉哪个字段正在执行表之间的关系,而不指定ID,只告诉“这个table1.ID等于这个表2 。ID”。然后在WHERE子句中添加过滤。所以,IMO,查询应该是这样的:

 $sql='SELECT M.movie_name, MC.on_screen_name, R.role, C.celebrity_name, C.celebrity_id, MI.production, MI.director,'
            . ' MI.screenplay, MI.music, MI.bgm_score, Col.movie_running_time, L.language, CC.censor_certificate,'
            . ' MR.movie_review_comment, A.award_name'
            . ' FROM tbl_movie M '
            . ' INNER JOIN tbl_movie_awards MA'
            . ' INNER JOIN tbl_movie_details MD'
            . ' INNER JOIN tbl_movie_cast MC ON MC.movie_id = M.movie_id'
            . ' INNER JOIN tbl_actor_role R ON MC.movie_role_id = R.role_id AND MC.movie_id = M.movie_id'
            . ' INNER JOIN tbl_celebrity C ON MC.movie_celebrity_id = C.celebrity_id AND MC.movie_id = M.movie_id'
            . ' INNER JOIN tbl_awards A ON MA.award_id = A.award_id AND MA.award_movie_id = M.movie_id
            . ' INNER JOIN tbl_language L ON MD.movie_language_id = L.language_id'
            . ' INNER JOIN tbl_censor_certificate CC ON MD.censor_id = CC.censor_id AND MD.movie_id = M.movie_id'
            . ' INNER JOIN tbl_movie_info MI ON MI.movie_info_id = M.movie_id'
            . ' INNER JOIN tbl_movie_collection Col ON Col.movie_id = M.movie_id'
            . ' INNER JOIN tbl_tt_movie_review MR ON MR.movie_review_id = M.movie_id'
            . ' WHERE M.movie_id = '.$movie_id.';';