mysql - 在 LEFT JOIN 之前使用变量值限制 MySQL 子查询

标签 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}

为了存档,我加入了一个无用“表”,只是为了更新@pid (parentid) 变量。

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

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

最佳答案

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

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

因为我使用的是 PHP,所以我在这里发布了针对这种情况的 MySQL 和 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,
                               ];
      }

如您所见,这也可以用于评论(而不是帖子)和子评论(而不是评论)。这次 MySQL 变量没有帮助。当然,要创建分页,您必须在表格中添加额外的字段(replies)并在评论创建期间更新它。

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

关于mysql - 在 LEFT JOIN 之前使用变量值限制 MySQL 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675810/

相关文章:

mysql - 设置了只读的从属服务器上存储过程中的临时表

mysql - 从MYSQL中工资第三高的员工表中获取所有员工工资

php - MySQL 几个内部连接与子查询

php - 对复选框设置限制,当达到限制时提交按钮将显示 PHP 和 jQuery

css - 限制CSS中按钮的宽度

mysql - where 子句中的计算列 - 性能

php - 将当前记录与先前记录进行比较

postgresql - Postgres - 简化基于日期的选择子查询的重复

mysql - SQL查询添加与子查询相关的where

java - 加载 100 条记录 10 x 10 jtable java 的文本文件?