在LEFT JOIN

时间:2018-04-05 15:10:25

标签: mysql subquery limit

我有这个MySQL表posts

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

这只是stackoverflow的一个示例

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

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"表"只是为了更新@pid(parentid)变量。

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

如果我必须在上面的示例中限制posts而不影响注释LIMIT,该怎么办?你能给我一个更好的询问吗?

1 个答案:

答案 0 :(得分:1)

发布此问题的真正原因是为每条评论加载10条评论和10条子评论。关于我要求加载帖子和问题的问题评论所以这个想法是一样的。

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

因为我正在使用PHP,所以我在这里发布MySQL和Linux的解决方案PHP适用于这种情况。

1 - 首先让我们使用此SQL查询加载帖子

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   = [];
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代码后,让我们为每个帖子存储评论:

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

正如您所看到的,这也可用于评论(而不是帖子)&amp;子评论(而不是评论)。 MySQL变量这次没用。当然要创建分页,您必须在表格中添加其他字段(replies)并在评论创建过程中更新。

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