例如我有这些表......
ID | SCORE | DATE
1 | 10 | 2-2-2012
2 | 20 | 2-2-2012
3 | 20 | 2-2-2012
2 | 5 | 4-2-2012
4 | 30 | 4-5-2012
1 | 20 | 5-5-2012
我希望得到每个ID的分数和最新日期。
我的期望输出是......
ID | SCORE | DATE
1 | 20 | 5-5-2012
2 | 5 | 4-2-2012
3 | 20 | 2-2-2012
4 | 30 | 4-5-2012
有可能吗?
提前致谢!
答案 0 :(得分:2)
SELECT t1.*
FROM tbl t1
INNER JOIN (SELECT MAX(date) max_date,
id
FROM tbl
GROUP BY id) t2 ON t1.id = t2.id
AND t1.date = t2.max_date
答案 1 :(得分:2)
尝试
SELECT id, score, date
FROM table1 t1
WHERE date=(SELECT MAX(t2.date)
FROM table1 t2
WHERE t1.id = t2.id);
答案 2 :(得分:0)
如果两个具有相同ID的行具有相同的日期,则如果您不关心获得案例的最高分:
select *,max(DATE) as max_date FROM table group by ID order by max_date desc;