PHP& MySQL分页显示问题

时间:2010-09-17 00:21:15

标签: php mysql pagination

当我设置我的分页以一次显示10条评论时,我的评论查询不会将评论回复计为显示计数的一部分,我该如何解决此问题以便我的评论回复计算在内?我的评论回复查询嵌套在我的主查询中以显示评论。

查询分页

SELECT COUNT(comment_id) FROM comments WHERE id = $id

显示评论的主要查询。

$dbc = mysqli_query($mysqli,"SELECT comments.*, users.*
                            FROM comments
                            LEFT JOIN users 
                            ON comments.user_id = users.user_id
                            WHERE id = '" . $id . "'
                            AND parent_comment_id = 0
                            LIMIT $start, $display");

主要查询回复评论。

//display comments replies
$dbc2 = mysqli_query($mysqli, "SELECT comments.*, users.*
                               FROM comments
                               LEFT JOIN users 
                               ON comments.user_id = users.user_id
                               WHERE id = '" . $id . "'
                               AND parent_comment_id >= 1");


//display comments replies
$dbc3 = mysqli_query($mysqli, "SELECT comments.*, users.*
                              FROM comments
                              LEFT JOIN users 
                              ON comments.user_id = users.user_id
                              WHERE id = '" . $id . "'
                              AND parent_comment_id >= 1");

1 个答案:

答案 0 :(得分:0)

也许这个查询,你能给出精确的数据库结构吗?

SELECT 
    com1.*, 
    users.*,
    (
        SELECT COUNT(*)
        FROM comments AS com2
        WHERE com2.parent_comment_id = com1.id
    ) AS num_replies
FROM comments AS com1
LEFT JOIN users ON com1.user_id = users.user_id
WHERE 
        com1.id = '" . $id . "'
    AND com1.parent_comment_id = 0
LIMIT $start, $display