避免重复结果mysql和php

时间:2012-09-29 05:21:17

标签: php mysql duplicates distinct

我正在创建一个带有回复功能的评论系统,而且我已经看到遇到了一个问题。

评论表

id        post_id        comment          replyTo

0           17            Blah               2

1           17            hello              2

2           17            goodbye         

3           17            great              1

4           17            bad

我的代码

$comments = mysql_query("SELECT * FROM comment WHERE post_id='$post_id' ORDER BY id DESC");

while($comment = mysql_fetch_assoc($comments))
{
    $id = $comment['id'];

    $comment = $comment['comment'];

    $reply = $comment['replyTo'];

    echo $comment;

    echo "<br />";


    $replyQuery = mysql_query("SELECT * FROM comment WHERE replyTo='$id' ORDER BY id DESC");

    while($comment = mysql_fetch_assoc($comments))

    {

        $id = $comment['id'];

        $comment = $comment['comment'];

        $reply = $comment['replyTo'];

        echo $comment;

        echo "<br />";

     }

}

我的结果

Blah

great

hello

goodbye

      blah

      hello

great

bad

预期结果

goodbye

      blah

      hello

      great

bad

感谢!!!!!

4 个答案:

答案 0 :(得分:0)

第一行应为:

$comments = mysql_query("SELECT * FROM comment WHERE post_id='$post_id' AND replyTo IS NULL ORDER BY id DESC");

这样只包含顶级评论。

答案 1 :(得分:0)

尝试此查询,它将在单个查询中包含所有结果。

  

SELECT GROUP_CONCAT(id)AS id,GROUP_CONCAT(post_id)AS post_id,GROUP_CONCAT(comment)AS comment,GROUP_CONCAT(replyTo)AS replyTo   FROM comment WHERE post_id = 17 GROUP BY replyTo ORDER BY id DESC

结果看起来像

id    post_id   comment      replyTo

3      17           great           1

2,4    17,17        goodbye,bad    0,0

0,1    17,17        Blah,hello     2,2

我不清楚你的要求。所以这只是一个建议......

答案 2 :(得分:0)

<?php
$comments = mysql_query("SELECT GROUP_CONCAT(id) AS id,GROUP_CONCAT(post_id) AS     post_id,GROUP_CONCAT(comment) AS comment,GROUP_CONCAT(date) AS date,GROUP_CONCAT(submittedBy)  AS submittedBy,GROUP_CONCAT(replyTo) AS replyTo FROM comment WHERE post_id='$post_id' GROUP BY replyTo ORDER BY id DESC");
while($comment = mysql_fetch_assoc($comments))
{
$cid = $comment['id'];
$comment_submit = $comment['submittedBy'];
$submitted_comment = $comment['comment'];
$comment_date = $comment['date'];
$replyTo = $comment['replyTo'];

echo'<div id="c'.$id.'" class="comment_container">';
    echo'<div class="comment_header">';
        echo'<a href="#" class="report">Flag</a>';
        echo'<a href="#" class="reply">Reply</a>';
        echo'<span>';
        echo $comment_submit.' - '.$comment_date.'</span>';
    echo'</div>';
    echo'<p>';
        echo $submitted_comment;
    echo'</p>';
    echo'
    <form method="post" class="replyForm">
        <textarea name="post_data_comment" class="replyTextArea" placeholder="@'.$comment_submit.'"></textarea>
        <input type="hidden" name="post_id" value="'.$post_id.'" />
        <input type="hidden" name="replyTo" value="'.$cid.'" />
        <div class="row_three">
            <span class="char_left_reply">Characters left: <span>500</span></span>
            <input type="button" value="Reply" name="share_reply" class="reply_comment" />
        </div>
    </form>
    ';
echo'</div>';

$reply = mysql_query("SELECT * FROM comment WHERE replyTo='$cid' ORDER BY id DESC");
while($replies = mysql_fetch_assoc($reply))
{
    $id = $replies['id'];
    $comment_submit = $replies['submittedBy'];
    $submitted_comment = $replies['comment'];
    $comment_date = $replies['date'];
    $replyTo = $replies['replyTo'];

    echo'<div id="c'.$id.'" class="comment_container" style="width:90%; float:right" >';
        echo'<div class="comment_header">';
            echo'<a href="#" class="report">Flag</a>';
            echo'<a href="#" class="reply">Reply</a>';
            echo'<span>';
            echo $comment_submit.' - '.$comment_date.'</span>';
        echo'</div>';
        echo'<p>';
            echo $submitted_comment;
        echo'</p>';
        echo'
        <form method="post" class="replyForm">
            <textarea name="post_data_comment" class="replyTextArea" placeholder="@'.$comment_submit.'"></textarea>
            <input type="hidden" name="post_id" value="'.$post_id.'" />
            <input type="hidden" name="replyTo" value="'.$cid.'" />
            <div class="row_three">
                <span class="char_left_reply">Characters left: <span>500</span></span>
                <input type="button" value="Reply" name="share_reply" class="reply_comment" />
            </div>
        </form>
        ';
    echo'</div>';
}

} ?&GT;

答案 3 :(得分:0)

请尝试

$comments = mysql_query("SELECT distinct(comment),id,replyTo FROM comment WHERE post_id='$post_id' ORDER BY id DESC");

while($comment = mysql_fetch_assoc($comments))
{
    $id = $comment['id'];

    $comment = $comment['comment'];

    $reply = $comment['replyTo'];

    echo $comment;

    echo "<br />";


    $replyQuery = mysql_query("SELECT * FROM comment WHERE replyTo='$id' and comment <>  '$comment' ORDER BY id DESC");

    while($comment = mysql_fetch_assoc($comments))

    {

        $id = $comment['id'];

        $comment = $comment['comment'];

        $reply = $comment['replyTo'];

        echo $comment;

        echo "<br />";

     }

}