数据库
首页 > 数据库> > mysql-在INNER JOIN中使用MAX-SQL

mysql-在INNER JOIN中使用MAX-SQL

作者:互联网

我有两个表,一个表名为facebook_posts,另一个表为facebook_post_metrics.

facebook_posts看起来像

NAME    id                      
a         1           
b         1            
c         4             
d         4             

facebook_post_metrics看起来像

number    FBID       Date_Executed             User_Executed
1         1        2012-09-18 16:10:44.917   admin
2         1        2012-09-25 11:39:01.000   jeff
3         4        2012-09-25 13:20:09.930   steve
4         4        2012-09-25 13:05:09.953   marsha

因此,用于内部联接的公共列是来自facebook_posts表的id和来自facebook_post_metrics的FBID.

因此,在内部Join之后,该表应如下所示:

  name  number           FBID            Date_Executed             User_Executed
   a       1               1             2012-09-18 16:10:44.917   admin
   b       2               1             2012-09-25 11:39:01.000   jeff
   c       3               4             2012-09-25 13:20:09.930   steve
   d       4               4             2012-09-25 13:05:09.953   marsha

但是,我想在进行此内部联接时包括其他条件.
基本上,我只想拥有上面连接表的最新条目.我知道我会使用max(date_executed),然后按FBID对其进行分组.但是我不确定使用INNER JOIN时会进入SQL查询的哪一部分.请帮帮我.

最重要的是…我想得到一张看起来像这样的桌子:

  name  number           FBID            Date_Executed             User_Executed
   b       2               1             2012-09-25 11:39:01.000   jeff
   c       3               4             2012-09-25 13:20:09.930   steve

解决方法:

遇到这样的问题,我建议将其分解并重新组合.

查找最近的facebook_posts_metrics行的日期很容易,如下所示:

SELECT fbid, MAX(date_executed) AS latestDate
FROM facebook_post_metrics
GROUP BY fbid;

因此,要获得整行,您需要将原始表与这些结果结合起来:

SELECT fpm.*
FROM facebook_post_metrics fpm
JOIN(
  SELECT fbid, MAX(date_executed) AS latestDate
  FROM facebook_post_metrics
  GROUP BY fbid) t ON t.fbid = fpm.fbid AND t.latestDate = fpm.date_executed;

最后,您要做的就是将其与facebook_posts表连接起来以获取名称:

SELECT fp.name, fpm.number, fpm.fbid, fpm.date_executed, fpm.user_executed
FROM facebook_posts fp
JOIN(
  SELECT fpm.*
  FROM facebook_post_metrics fpm
  JOIN(
    SELECT fbid, MAX(date_executed) AS latestDate
    FROM facebook_post_metrics
    GROUP BY fbid) t ON t.fbid = fpm.fbid AND t.latestDate = fpm.date_executed
  ) fpm ON fpm.fbid = fp.id AND fpm.date_executed = fp.updated_at;

这是一个SQL Fiddle示例.

编辑

根据您的评论并仔细研究您的设计,我相信您可以做这样的事情.首先,获取我上面描述的最新的facebook_post_metrics.然后,使用类似的方法获取最新的facebook_post.这将搜索Facebook帖子的最新updated_at值.如果要使用其他日期列,只需更改以下内容:

SELECT fp.*
FROM facebook_posts fp
JOIN(
  SELECT id, MAX(updated_at) AS latestUpdate
  FROM facebook_posts
  GROUP BY id) t ON t.id = fp.id AND t.latestUpdate = fp.updated_at;

最后,您可以在id和fbid列匹配的情况下,将该查询与facebook_post_metrics的查询一起加入:

SELECT fp.name, fpm.number, fpm.fbid, fpm.date_executed, fpm.user_executed
FROM(
  SELECT fp.*
  FROM facebook_posts fp
  JOIN(
    SELECT id, MAX(updated_at) AS latestUpdate
    FROM facebook_posts
    GROUP BY id) t ON t.id = fp.id AND t.latestUpdate = fp.updated_at) fp
JOIN(
  SELECT fpm.*
  FROM facebook_post_metrics fpm
  JOIN(
    SELECT fbid, MAX(date_executed) AS latestDate
    FROM facebook_post_metrics
    GROUP BY fbid) t ON t.fbid = fpm.fbid AND t.latestDate = fpm.date_executed) fpm
ON fp.id = fpm.fbid;

这是更新的SQL Fiddle示例.

标签:join,inner-join,max,mysql,sql-server
来源: https://codeday.me/bug/20191120/2047055.html