2012-04-27 50 views
0
$rget_comments = mysql_query("SELECT DISTINCT comment.id, comment.comment_name, comment.comment_body FROM rs_comments AS comment WHERE comment.comment_parent ='0' ORDER BY comment.id ASC"); 
while($get_comment = mysql_fetch_array($rget_comments)) 
{ 

    echo '+ <a href="comment.php?id=' . $get_comment['id'] . '">' . $get_comment['comment_name'] . '</a><br />'; 
    echo $get_comment['body'] . '<br />'; 

    $rget_comments = mysql_query("SELECT DISTINCT 
    (sub_comment.id) AS sub_id, (sub_comment.comment_name) AS sub_comment_name, (sub_comment.comment_body) AS sub_comment_body 
    FROM rs_comments AS sub_comment WHERE sub_comment ON sub_comment.comment_parent = '1'"); 
    while($get_comment = mysql_fetch_array($rget_comments)) 
    { 

     echo '- <a href="comment.php?id=' . $get_comment['sub_id'] . '">' . $get_comment['sub_comment_name'] . '</a><br />'; 
     echo $get_comment['sub_body'] . '<br />'; 
    } 

} 

我的表信息:如何在一個查詢中使用這兩個選擇查詢

CREATE TABLE IF NOT EXISTS `rs_comments` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `comment_type` varchar(255) NOT NULL, 
    `comment_post_ID` int(11) NOT NULL, 
    `comment_parent` int(11) NOT NULL, 
    `comment_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '', 
    `comment_body` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; 

INSERT INTO `rs_comments` (`id`, `comment_type`, `comment_post_ID`, `comment_parent`, `comment_name`,`comment_body`) VALUES 
(1, 'post', 1, 0, 'test comment', 'test comment body'), 
(2, 'post', 1, 0, 'test comment 2', 'test comment 2 body'), 
(3, 'post', 1, 1, 'sub comment 1', 'sub comment 1 body'), 
(4, 'post', 1, 1, 'sub comment 2', 'sub comment 2 body'); 

我有一個查詢,但只會返回第一行:

$rget_comments = mysql_query(" 
    SELECT DISTINCT 
    comment.id, comment.comment_name, comment.comment_body, (sub_comment.id) AS sub_id, (sub_comment.comment_name) AS sub_comment_name, (sub_comment.comment_body) AS sub_comment_body 
    FROM rs_comments AS comment 
    LEFT OUTER JOIN rs_comments AS sub_comment ON sub_comment.comment_parent = comment.id 
    WHERE comment.comment_parent ='0' 
    GROUP BY comment.id 
    ORDER BY comment.id ASC"); 
    while($get_comment = mysql_fetch_array($rget_comments)) 
    { 
     echo '+ <a href="comment.php?id=' . $get_comment['id'] . '">' . $get_comment['comment_name'] . '</a><br />'; 
     echo $get_comment['body'] . '<br />'; 

     echo '- <a href="comment.php?id=' . $get_comment['sub_id'] . '">' . $get_comment['sub_comment_name'] . '</a><br />'; 
     echo $get_comment['sub_body'] . '<br />'; 
    } 

結果:

+test comment 
test comment 1 body 
-sub comment 1 
sub comment 1 body 
+test comment 2 
test comment 2 body 

預期結果:

+test comment 
test comment 1 body 
-sub comment 1 
sub comment 1 body 
-sub comment 2 
sub comment 2 body 
+test comment 2 
test comment 2 body 
+0

你能說XSS? – PeeHaa 2012-04-27 20:03:45

+1

您的查詢是返回兩行,還是PHP只是沒有回顯它們兩個。當我嘗試這個代碼時,兩者都適用於我。 – GDP 2012-04-27 20:06:59

+1

對不起,我不是英語 好還不行我 結果應該如下: +測試評論 測試註釋1分體 -sub評論1 子評論1體 -sub評論2 子註釋2體 +測試註釋2 測試註釋2體 但是現在是這樣的: +測試評論 測試註釋1分體 -sub評論1 子評論1體 +測試註釋2 測試註釋2體 – S6Stable 2012-04-27 20:17:47

回答

1

GROUP BY comment.id導致此問題。 GROUP BY將具有相同id和不同sub_id的兩條記錄分組到一條記錄。像這樣刪除它:

SQL:

SELECT DISTINCT 
    comment.id, comment.comment_name, comment.comment_body, (sub_comment.id) AS sub_id, (sub_comment.comment_name) AS sub_comment_name, (sub_comment.comment_body) AS sub_comment_body 
    FROM rs_comments AS comment 
     LEFT OUTER JOIN rs_comments AS sub_comment ON sub_comment.comment_parent = comment.id 
    WHERE comment.comment_parent ='0' 
    ORDER BY comment.id ASC 

PHP:

$flag = array(); 
while($get_comment = mysql_fetch_array($rget_comments)) { 
    if(!isset($flag[$get_comment['id']])) { 
     echo '+ <a href="comment.php?id=' . $get_comment['id'] . '">' . $get_comment['comment_name'] . '</a><br />'; 
     echo $get_comment['body'] . '<br />'; 
     $flag[$get_comment['id']] = true; 
    } 
    if($get_comment['sub_id']) { 
     echo '- <a href="comment.php?id=' . $get_comment['sub_id'] . '">' . $get_comment['sub_comment_name'] . '</a><br />'; 
     echo $get_comment['sub_body'] . '<br />'; 
    } 
} 
+0

個謝謝,曾任職 但是,這將導致: +測試評論 測試註釋1體 -sub評論1 子評論1體 +測試評論 測試註釋1分體 -sub評論2 子註釋2體 +測試評論2 測試評論2身體 – S6Stable 2012-04-27 21:33:03

+0

真的嗎?我只是製作桌子並在本地確認,它就可以。請確認查詢是否在您的實際代碼的每個位置更新 – tosin 2012-04-27 21:40:18

+0

對不起,我英文不好,但是有效,但是對於每個子評論,主要評論重複 – S6Stable 2012-04-27 21:53:27