<small id='g7Gk5'></small><noframes id='g7Gk5'>

<tfoot id='g7Gk5'></tfoot>
  • <i id='g7Gk5'><tr id='g7Gk5'><dt id='g7Gk5'><q id='g7Gk5'><span id='g7Gk5'><b id='g7Gk5'><form id='g7Gk5'><ins id='g7Gk5'></ins><ul id='g7Gk5'></ul><sub id='g7Gk5'></sub></form><legend id='g7Gk5'></legend><bdo id='g7Gk5'><pre id='g7Gk5'><center id='g7Gk5'></center></pre></bdo></b><th id='g7Gk5'></th></span></q></dt></tr></i><div id='g7Gk5'><tfoot id='g7Gk5'></tfoot><dl id='g7Gk5'><fieldset id='g7Gk5'></fieldset></dl></div>

      <bdo id='g7Gk5'></bdo><ul id='g7Gk5'></ul>
      <legend id='g7Gk5'><style id='g7Gk5'><dir id='g7Gk5'><q id='g7Gk5'></q></dir></style></legend>
      1. MySQL限制LEFT JOIN加入后的子查询

        时间:2023-10-09

        <small id='IIXUx'></small><noframes id='IIXUx'>

        • <bdo id='IIXUx'></bdo><ul id='IIXUx'></ul>
            <tbody id='IIXUx'></tbody>

            1. <tfoot id='IIXUx'></tfoot>

              <legend id='IIXUx'><style id='IIXUx'><dir id='IIXUx'><q id='IIXUx'></q></dir></style></legend>
            2. <i id='IIXUx'><tr id='IIXUx'><dt id='IIXUx'><q id='IIXUx'><span id='IIXUx'><b id='IIXUx'><form id='IIXUx'><ins id='IIXUx'></ins><ul id='IIXUx'></ul><sub id='IIXUx'></sub></form><legend id='IIXUx'></legend><bdo id='IIXUx'><pre id='IIXUx'><center id='IIXUx'></center></pre></bdo></b><th id='IIXUx'></th></span></q></dt></tr></i><div id='IIXUx'><tfoot id='IIXUx'></tfoot><dl id='IIXUx'><fieldset id='IIXUx'></fieldset></dl></div>
                  本文介绍了MySQL限制LEFT JOIN加入后的子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  目前我有这个查询:

                  SELECT post.id AS postID, sCom.id as CommentID FROM `post` LEFT JOIN (SELECT * FROM `comment` LIMIT 5) AS sCom ON sCom.post_id = post.id;

                  输出:

                  postID |评论号1 |12 |空值3 |空值4 |25 |35 |45 |5

                  它有效,但它在加入之前限制了评论表.结果是,它选择了前 5 条评论并将其映射.id 为 5 的所有评论都会被忽略.

                  如何重写查询以选择最多 5 条评论的帖子?

                  当前表结构:

                  发布:

                  <前>如果不存在`post`,则创建表(`id` int(11) NOT NULL AUTO_INCREMENT,`feed_id` int(11) 默认为空,`user_id` int(11) 默认为空,`origin_id` int(11) 默认为空,`content` longtext COLLATE utf8_unicode_ci NOT NULL,`已启用` tinyint(1) 非空,`created_at` 日期时间非空,`updated_at` 日期时间非空,主键(`id`),密钥`IDX_5A8A6C8D51A5BC03`(`feed_id`),密钥`IDX_5A8A6C8DA76ED395`(`user_id`),密钥`IDX_5A8A6C8D56A273CC`(`origin_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

                  评论:

                  <前>如果不存在评论",则创建表(`id` int(11) NOT NULL AUTO_INCREMENT,`feed_id` int(11) 默认为空,`user_id` int(11) 默认为空,`post_id` int(11) 默认为空,`content` longtext COLLATE utf8_unicode_ci NOT NULL,`已启用` tinyint(1) 非空,`created_at` 日期时间非空,`updated_at` 日期时间非空,主键(`id`),密钥`IDX_9474526C51A5BC03`(`feed_id`),密钥`IDX_9474526CA76ED395`(`user_id`),密钥`IDX_9474526C4B89032C`(`post_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11 ;

                  谢谢

                  解决方案

                  这将为您的每篇文章提供 5 条评论.

                  SELECT p.*,C.*发件人左加入(选择一个.*FROM 评论 a在哪里(选择 COUNT(*)FROM 评论 bWHERE a.Post_ID = b.Post_ID ANDa.ID <= b.ID) <= 5) c ON a.ID = c.Post_ID

                  currently i have this Query:

                  SELECT post.id AS postID, sCom.id as CommentID FROM `post` LEFT JOIN (SELECT * FROM `comment` LIMIT 5) AS sCom ON sCom.post_id = post.id;
                  

                  Output:

                  postID | CommentID
                  1      | 1
                  2      | null
                  3      | null
                  4      | 2
                  5      | 3
                  5      | 4
                  5      | 5
                  

                  It works but it LIMITs the comment Table before JOINing. The result is, that it selects the first 5 comments and maps it. All comments over an id of 5 gets ignored.

                  How can i rewrite the query to have The post with maximum of 5 comments selected ?

                  The current table structure:

                  Post :

                  CREATE TABLE IF NOT EXISTS `post` (
                   `id` int(11) NOT NULL AUTO_INCREMENT,
                   `feed_id` int(11) DEFAULT NULL,
                   `user_id` int(11) DEFAULT NULL,
                   `origin_id` int(11) DEFAULT NULL,
                   `content` longtext COLLATE utf8_unicode_ci NOT NULL,
                   `enabled` tinyint(1) NOT NULL,
                   `created_at` datetime NOT NULL,
                   `updated_at` datetime NOT NULL,
                   PRIMARY KEY (`id`),
                   KEY `IDX_5A8A6C8D51A5BC03` (`feed_id`),
                   KEY `IDX_5A8A6C8DA76ED395` (`user_id`),
                   KEY `IDX_5A8A6C8D56A273CC` (`origin_id`)
                  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;
                  

                  Comment:

                  CREATE TABLE IF NOT EXISTS `comment` (
                   `id` int(11) NOT NULL AUTO_INCREMENT,
                   `feed_id` int(11) DEFAULT NULL,
                   `user_id` int(11) DEFAULT NULL,
                   `post_id` int(11) DEFAULT NULL,
                   `content` longtext COLLATE utf8_unicode_ci NOT NULL,
                   `enabled` tinyint(1) NOT NULL,
                   `created_at` datetime NOT NULL,
                   `updated_at` datetime NOT NULL,
                   PRIMARY KEY (`id`),
                   KEY `IDX_9474526C51A5BC03` (`feed_id`),
                   KEY `IDX_9474526CA76ED395` (`user_id`),
                   KEY `IDX_9474526C4B89032C` (`post_id`)
                  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11 ;
                  

                  Thanks

                  解决方案

                  This will give you 5 comments for every post.

                  SELECT  p.*,
                          c.*
                  FROM    Post p
                          LEFT JOIN
                          (
                              SELECT  a.*
                              FROM    Comments a
                              WHERE    
                                      (
                                         SELECT   COUNT(*) 
                                         FROM     Comments b
                                         WHERE    a.Post_ID = b.Post_ID AND 
                                                  a.ID <= b.ID
                                      ) <= 5
                          ) c ON  a.ID = c.Post_ID
                  

                  这篇关于MySQL限制LEFT JOIN加入后的子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:INNER JOIN 和 LEFT/RIGHT OUTER JOIN 的问题 下一篇:在 SQLite 中合并两个表

                  相关文章

                    1. <legend id='hU8HB'><style id='hU8HB'><dir id='hU8HB'><q id='hU8HB'></q></dir></style></legend>

                      <tfoot id='hU8HB'></tfoot>
                      <i id='hU8HB'><tr id='hU8HB'><dt id='hU8HB'><q id='hU8HB'><span id='hU8HB'><b id='hU8HB'><form id='hU8HB'><ins id='hU8HB'></ins><ul id='hU8HB'></ul><sub id='hU8HB'></sub></form><legend id='hU8HB'></legend><bdo id='hU8HB'><pre id='hU8HB'><center id='hU8HB'></center></pre></bdo></b><th id='hU8HB'></th></span></q></dt></tr></i><div id='hU8HB'><tfoot id='hU8HB'></tfoot><dl id='hU8HB'><fieldset id='hU8HB'></fieldset></dl></div>

                      • <bdo id='hU8HB'></bdo><ul id='hU8HB'></ul>

                      <small id='hU8HB'></small><noframes id='hU8HB'>