简体   繁体   English

MySql-三个成功连接的表,但未获得一列结果?

[英]MySql - three sucessfully joined tables, but failing to get one column result?

I have three tables (user, post, comments) joined up (for an ajax powered social network thing I am doing for a degree). 我有三个表(用户,帖子,评论)连接在一起(对于我正在做一个学位的ajax社交网络来说)。

So far so good but when I loop through them I am getting all the results except one. 到目前为止,一切都很好,但是当我遍历它们时,我得到了除一个以外的所有结果。 The reference to who commented on a post (stored within comments table) is coming up only as a number id (which should be joined to the user table but my join is obviously not working correctly!). 关于谁对帖子(存储在评论表中)发表评论的引用仅以数字ID出现(应该将其加入到用户表中,但我的加入显然无法正常工作!)。

Any help very much appreciated! 任何帮助非常感谢! Probably something simple as I am still new at this PHP/mysql game! 可能有些简单,因为我还是这个PHP / mysql游戏的新手!

MySQL tables: MySQL表:

           CREATE TABLE `post` (
           `pid` int(10) NOT NULL AUTO_INCREMENT,
           `uid` int(10) NOT NULL,
           `post` text NOT NULL,
           `pid_imageurl` varchar(100) NOT NULL,
           `likes` int(10) NOT NULL,
            PRIMARY KEY (`pid`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 



           CREATE TABLE `user` (
           `uid` int(10) NOT NULL AUTO_INCREMENT,
           `name` varchar(20) NOT NULL,
           `password` varchar(20) NOT NULL,
           `uid_imageurl` varchar(100) NOT NULL,
           `joindate` varchar(11) NOT NULL,
           PRIMARY KEY (`uid`)
           ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;


           CREATE TABLE `comments` (
           `cid` int(10) NOT NULL AUTO_INCREMENT,
           `comment_pid` int(10) NOT NULL,
           `comment_uid` int(10) NOT NULL,
           `comment` text NOT NULL,
            PRIMARY KEY (`cid`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

Code: 码:

        $sql = "SELECT post.post, post.pid_imageurl, post.likes, user.name, comments.comment,   
        user.uid_imageurl, comments.comment_uid

                FROM post
                INNER JOIN user
                ON post.uid=user.uid
                LEFT JOIN comments
                ON comments.comment_pid=post.pid
                AND user.uid=comments.comment_uid
                ";

        $result = mysql_query($sql);



        while($row=mysql_fetch_assoc($result)){?>
        <ul>
            <li>User Profile image here:<? echo $row['uid_imageurl']; ?></li>
            <li>Post:<? echo $row['post']; ?></li>
            <li>Post by:<? echo $row['name']; ?></li>
            <li>Post images:<? echo $row['pid_imageurl']; ?></li>

            <li>post has: <? echo $row['likes']; ?> likes.</li>
            <li><? echo $row['comment']; ?></li>
            <li>by: <? echo $row['comment_uid']; ?></li>
           <!--This is only output as a stored id no rather than users name--!>




        </ul>


        <?}
        ?>

First, your query should be this: 首先,您的查询应为:

SELECT 
    p.post, 
    p.pid_imageurl, 
    p.likes, 
    u.name, 
    c.comment,   
    u.uid_imageurl, 
    cu.name as CommentName
FROM 
    post p
    INNER JOIN user u ON 
        p.uid=u.uid
    LEFT JOIN comments c ON 
        c.comment_pid=post.pid
    left join user cu on
        c.comment_uid = cu.uid

And $row['comment_uid'] should become $row['CommentName'] . 并且$row['comment_uid']应该变成$row['CommentName']

Your original query is only grabbing comments that the original poster has made (your join condition). 您的原始查询仅获取原始发帖人所做的评论(您的加入条件)。 You want to join to the user table yet again to get the comment poster rather than the original poster. 您想再次加入user表以获取评论发布者,而不是原始发布者。

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

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