SQL查询以查找多个ID的最新时间戳

时间:2015-11-11 16:31:00

标签: sql join max inner-join

我正在尝试运行一个查询,该查询返回某个用户组执行的最后一个“游戏”或操作。我不得不加入几个表来最终获得我想要的东西,但这基本上会返回每个用户的游戏。我想参加他们的最后一场比赛。我在查询中添加了max(___)到时间戳,但最终只返回了最近玩过的游戏,就是这样。理想情况下,我希望看到每个用户及其最后一次行动。

-module(example_controller, [Req]).
-compile(export_all).


% Get parameters 
subscribe('GET', []) ->
%Split into parameters to get verify_token 
% Set value of hub.challenge to Challenge variable    
RequestBody = string:tokens(binary_to_list(Req:request_body()), "&"),
cowboy_req:reply(200, Challenge).

1 个答案:

答案 0 :(得分:0)

以下SQL语句为您提供每个团队的最新时间戳/操作:

SELECT MAX(timestamp) as latestTimestamp, homeTeamId
  FROM test_results
  GROUP BY homeTeamId

它可以与其他2个表联合

SELECT teamName, tt.userID, tt.timestamp AS signup, sponsorship, tr.latestTimestamp, stadium, win+draw+lose AS games 
FROM test_teams tt
INNER JOIN test_profile
  ON test_teams.userID = test_profile.userID
INNER JOIN (
  SELECT MAX(timestamp) as latestTimestamp, homeTeamId
  FROM test_results
  GROUP BY homeTeamId) tr 
  ON test_teams.userID = tr.homeTeamId