这是我表的一部分:
id team_id log_id 1 12 1 2 12 1 3 12 1 4 12 1 5 1 2 6 1 2 7 1 3 8 1 3
哪个查询会产生此输出(因此只返回与team_id相对应的log_id值最高的记录)?
id team_id log_id 1 12 1 2 12 1 3 12 1 4 12 1 7 1 3 8 1 3
答案 0 :(得分:2)
SELECT *
FROM mytable t
WHERE log_id = (SELECT MAX(log_id) FROM mytable WHERE team_id = t.team_id)
答案 1 :(得分:1)
SELECT id, team_id, log_id
FROM table1 t2
JOIN (SELECT team_id, MAX(log_id) max_log_id
FROM table1
GROUP BY team_id) t2 ON t1.team_id = t2.team_id
AND t1.log_id = t2.max_log_id