LIMIT MySQL 子查询在 LEFT JOIN 之前使用变量值

LIMIT MySQL subquery using variable value before LEFT JOIN

我有这个MySQLtableposts:

id | content       | parentid | userid
--------------------------------------
01 | Post test #1  | 0        | 1
02 | Post test #2  | 0        | 1
03 | Comment #1    | 1        | 2
04 | Comment #2    | 1        | 1
05 | Post test #3  | 0        | 3
06 | Comment #3    | 1        | 2
07 | Comment #4    | 2        | 5
08 | Comment #5    | 5        | 6
09 | Comment #6    | 1        | 4
10 | Post test #4  | 0        | 4

这只是 Whosebug 的一个例子

现在我需要限制每个 post 的评论,到目前为止我已经使用了这个查询:

SELECT
    `posts`.`id` AS `post_id`,
    `posts`.`content` AS `post_content`,
    `posts`.`parentid` AS `post_parentid`,
    `posts`.`userid` AS `post_userid,

    `comments`.`id`, 0 AS `comment_id`,
    `comments`.`content` AS `comment_content`,
    `comments`.`parentid` AS `comment_parentid`,
    `comments`.`userid` AS `comment_userid,
    IF( IFNULL( `comments`.`id`, 0 ) > 0, "comment", "post" ) AS `contenttype`

FROM `posts` AS `posts`
LEFT JOIN ( SELECT "" AS `hello` ) AS `useless` ON @pid := `posts`.`id`
LEFT JOIN ( SELECT
        `posts`.`id` AS `id`,
        `posts`.`id` AS `id`,
        `posts`.`id` AS `id`,
        `posts`.`id` AS `id`
    FROM `posts`
    WHERE `posts`.`parentid` = @pid
    LIMIT 10
    ) AS `comments`ON `comments`.`parentid` = `posts`.`id`
WHERE
    `posts`.`userid` = {USERID}

为了存档,我加入了一个 useless "table" 只是为了更新@pid (parentid) 变量。

这是限制子查询结果的唯一方法吗?我不喜欢 useless JOIN 的想法。

如果我必须在上面的示例中 LIMIT posts 而不影响评论 LIMIT 怎么办。你能给我一个更好的查询吗?

post这个问题的真正原因是加载 10 条评论,每条评论 10 sub-comments。关于我要求加载 posts 和评论的问题,所以想法是一样的。

我的问题中的示例 post 不起作用,因为子查询将在变量 @pid 更新之前执行。

因为我使用的是 PHP,所以我 post 在 MySQL 和 PHP 中找到了针对这种情况的解决方案。

1 - 首先让我们用这个 SQL 查询

加载 posts
SELECT
    `posts`.`id` AS `id`,
    `posts`.`content` AS `content`,
    `posts`.`parentid` AS `parentid`,
    `posts`.`userid` AS `userid

 FROM `posts` AS `posts`
 WHERE
    `posts`.`userid` = {USERID}
    AND
    `posts`.`parentid` = '0'
 ORDER BY `posts`.`id` DESC
 LIMIT 10

2 - 在 $posts 数组中存储 posts 信息:

$posts   = [];
while   ( $row = $result->fetch_object() )
        {
        $posts[] = (object) [ "id"       => $row->id,
                              "content"  => $row->content,
                              "userid"   => $row->userid,
                              "comments" => []
                              ];
        }

3 - 准备 SQL 加载评论:

$size    = count( $posts );
$sql     = "";
for     ( $i = 0; $i < $size; $i ++ )
        {
        $sql     .= ( $sql != "" ? "UNION ALL " : "" )
                 . "( "
                 . "SELECT "
                 . "`comments`.`id` AS `id`, "
                 . "`comments`.`content` AS `content`, "
                 . "`comments`.`parentid` AS `parentid`, "
                 . "`comments`.`userid` AS `userid "
                 . "FROM `posts` AS `comments` "
                 . "WHERE "
                 . "`comments`.`parentid` = '" . $post[ $i ]->id . "' "
                 . "ORDER BY `comments`.`id` ASC "
                 . "LIMIT 10 "
                 . ") ";
        }

4 - 执行 $sql 代码后,让我们为每个 post:

存储注释
while ( $row = $result->fetch_object() )
      {
      $posts[ $row->parentid ]->comments[] = (object)[
                               "id"       => $row->id,
                               "content"  => $row->content,
                               "userid"   => $row->userid,
                               ];
      }

如您所见,这也可用于评论(而不是 posts )和 sub-comments(而不是评论)。 MySQL 这次变量没有帮助。当然,要创建分页,您必须在 table 中添加其他字段 (replies) 并在创建评论期间更新它。

如果有人有更好的解决方案欢迎。