Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询问题:X有多个Y。获取所有X,每个X只获取最新的Y_Sql_Mysql_Database_Join - Fatal编程技术网

SQL查询问题:X有多个Y。获取所有X,每个X只获取最新的Y

SQL查询问题:X有多个Y。获取所有X,每个X只获取最新的Y,sql,mysql,database,join,Sql,Mysql,Database,Join,假设我们有两张桌子。张贴和评论。这篇帖子有很多评论。假装他们有点满了,所以每个帖子的评论数量是不同的。我想要一个查询,它将抓取所有帖子,但每个帖子只有最新的评论 我已经被引导到连接和子查询,但我无法理解 示例输出: Post1: 评论4(post1的最新版本) 职位2: 评论2(post2的最新版本) Post3: 评论10(post3的最新版本) 等等 任何帮助都将不胜感激。谢谢。这个答案假设你的每条评论都有一个唯一的标识符,而且这个数字在不断增加。也就是说,后面的帖子比前面的帖子数量多。不必

假设我们有两张桌子。张贴和评论。这篇帖子有很多评论。假装他们有点满了,所以每个帖子的评论数量是不同的。我想要一个查询,它将抓取所有帖子,但每个帖子只有最新的评论

我已经被引导到连接和子查询,但我无法理解

示例输出:

Post1: 评论4(post1的最新版本)

职位2: 评论2(post2的最新版本)

Post3: 评论10(post3的最新版本)

等等


任何帮助都将不胜感激。谢谢。

这个答案假设你的每条评论都有一个唯一的标识符,而且这个数字在不断增加。也就是说,后面的帖子比前面的帖子数量多。不必是顺序的,只需与输入顺序相对应

首先,执行一个查询,提取按帖子id分组的最大注释id

大概是这样的:

SELECT MAX(ID) MaxCommentID, PostID
FROM Comments
GROUP BY PostID
这将为您提供帖子id列表,以及每个帖子的最高(最新)评论id

然后你加入这个,为那些id从注释中提取其余的数据

SELECT C1.*, C2.PostID
FROM Comments AS C1
     INNER JOIN (
         SELECT MAX(ID) MaxCommentID, PostID
         FROM Comments
         GROUP BY PostID
     ) AS C2 ON C1.CommentID = C2.MaxCommentID
然后,您加入这些帖子,以获取关于这些帖子的信息

SELECT C1.*, P.*
FROM Comments AS C1
     INNER JOIN (
         SELECT MAX(ID) MaxCommentID, PostID
         FROM Comments
         GROUP BY PostID
     ) AS C2 ON C1.CommentID = C2.MaxCommentID
     INNER JOIN Posts AS P ON C2.PostID = P.ID

另一种方法根本不使用内部查询的PostID。首先,为所有独特的帖子选择最大评论id,但不要在意哪个帖子,我们知道它们是独特的

SELECT MAX(ID) AS MaxCommentID
FROM Comments
GROUP BY PostID
然后执行IN子句,以获取这些注释的其余数据:

SELECT C1.*
FROM Comments
WHERE C1.ID IN (
    SELECT MAX(ID) AS MaxCommentID
    FROM Comments
    GROUP BY PostID
)
然后简单地加入帖子:

SELECT C1.*, P.*
FROM Comments AS C1
     INNER JOIN Posts AS P ON C1.PostID = P.ID
WHERE C1.ID IN (
    SELECT MAX(ID) AS MaxCommentID
    FROM Comments
    GROUP BY PostID
)

从子查询中选择最新的注释

e、 g


如果您仍然使用旧的mysql版本,那么不知道子查询,您可以使用类似于select的东西 SELECT p.id, c1.id FROM posts as p LEFT JOIN comments as c1 ON p.id = c1.postId LEFT JOIN comments as c2 ON c1.postId = c2.postId AND c1.id < c2.id WHERE isnull(c2.id) ORDER BY p.id p、 id,c1.id 从…起 职位为p 左连接 作为c1的评论 在…上 p、 id=c1.posted 左连接 作为c2的评论 在…上 c1.postId=c2.postId c1.idp、 另一种方法是,检查您的查询是否存在性能问题。

如果帖子不一定要有评论,请在帖子和评论之间使用左外连接而不是内连接
 select *
   from post
      , comments
  where post.post_id = comments.post_id
    and comments.comments_id = (select max(z.comments_id) from comments z where z.post_id = post.post_id)
SELECT p.id, c1.id FROM posts as p LEFT JOIN comments as c1 ON p.id = c1.postId LEFT JOIN comments as c2 ON c1.postId = c2.postId AND c1.id < c2.id WHERE isnull(c2.id) ORDER BY p.id