在INNER JOIN中使用MAX - SQL

时间:2015-01-16 19:35:43

标签: mysql sql-server join max inner-join

我有两个表,一个叫做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。

因此在内部连接之后,表格应如下所示:

  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

3 个答案:

答案 0 :(得分:3)

如果遇到这样的问题,我建议将其拆分成几块并重新组合在一起。

查找最近的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;

最后,在idfbid列匹配的情况下,您可以将该查询与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示例。

答案 1 :(得分:0)

您需要subquery使用max计算group by,然后再次使用相同的表格来获取所有详细信息

根据最新编辑,发布中的update_at列不再存在 由于有两个条目,您只能通过

进行分组来获得一个条目

如果您想要所有条目,则删除外部查询中的聚合和group by

select max(fp.name), fpm.number, fpm.FBID, fpm.date_executed,
       fpm.user_executed
from facebook_posts fp
join ( select max(Date_executed) dexecuted, FBID
       from facebook_post_metrics 
       group by FBID
      ) t
on fp.id = t.fbid
join facebook_post_metrics fpm
on fpm.fbid = t.fbid
and fpm.date_executed = t.dexecuted
group by fpm.number, fpm.FBID, fpm.date_executed,
       fpm.user_executed

答案 2 :(得分:0)

未经测试,但您只需选择相关FBID的最大日期即可。像这样......

SELECT fb.name, fpm.number, fpm.FBID, fpm.date_executed, fpm.user_executed
FROM facebook_post_metrics fpm
    join facebook_posts fb ON fpm.fbid = fb.id AND fb.updated_at = fpm.date_executed
WHERE fpm.Date_Executed = (
SELECT MAX(f.Date_executed)
FROM facebook_post_metrics f
WHERE f.FBID = fpm.fbid)

这里有一个小提琴http://sqlfiddle.com/#!2/1485d/15