INNER JOIN和UNION的问题

时间:2017-01-19 04:30:38

标签: php mysql

以下查询返回此错误:

  

警告:mysqli_num_rows()要求参数1为mysqli_result,第21行的xxx.php中给出布尔值。   致命错误:在第24行的xxx.php中调用boolean上的成员函数fetch_assoc()

当我从两个SELECT语句中删除'userID'列时,查询似乎才有效。这与我在INNER JOIN中使用'userID'这一事实有什么关系吗?我该如何解决这个问题?在此先感谢。

QUERY;

$sql = "SELECT parentID, userID, Rating, Comment, upvote_count, Time FROM     ratings_n_comments
        INNER JOIN user_details
        ON ratings_n_comments.userID=user_details.UserID 
        WHERE ratings_n_comments.mID= '".$mID."'    
        UNION
        SELECT parentID, userID, Rating, Comment, upvote_count, Time FROM replys_to_comments
        INNER JOIN user_details
        ON replys_to_comments.userID=user_details.UserID 
        WHERE replys_to_comments.mID= '".$mID."'";

1 个答案:

答案 0 :(得分:0)

表user_details和ratings_n_comments具有相同的列名userID,因为模糊不会显示结果。所以,使用user_details.userID或 在select语句中的ratings_n_comments.userID就像我在下面使用

一样
"SELECT parentID, user_details.userID, Rating, Comment, upvote_count, Time FROM     ratings_n_comments
        INNER JOIN user_details
        ON ratings_n_comments.userID=user_details.UserID 
        WHERE ratings_n_comments.mID= '".$mID."'    
        UNION
        SELECT parentID, user_details.userID, Rating, Comment, upvote_count, Time FROM replys_to_comments
        INNER JOIN user_details
        ON replys_to_comments.userID=user_details.UserID 
        WHERE replys_to_comments.mID= '".$mID."'";
相关问题