mysql - >每年最大值与记录ID的信息

时间:2014-12-12 18:45:26

标签: php mysql

我想获取年度最大值和记录ID信息,以便与其他表格进一步连接。

考虑下表:

tur_id  Datum   SZ  Art VW  StV TV  NSP
189 23.06.2010  09:40:00    S   1   -37 -35 46
7   11.05.2012  08:40:00    S   1   -19,9   -21 45
140 02.07.2011  10:30:00    S   1   -25 -26 45
62  31.07.2013  31.07.2013  S   1   -16 -16 42
136 12.07.2011  11:20:00    S   1   -21,4   -23 41
181 04.08.2010  10:00:00    S   1   -30,1   -28 41
195 24.10.2009  09:40:00    S   1   -45 -47 41
90  22.10.2013  22.10.2013  S   1   -14,2   -16 40
11  16.06.2012  10:50:00    S   1   -17 -18 40
153 13.05.2011  09:25:00    S   1   -27,4   -29 40
1   23.07.2014  23.07.2014  S   1   -13,6   -14 39
56  15.06.2013  15.06.2013  S   1   -17,3   -18 39
45  26.10.2012  26.10.2012  S   1   -17,4   -18 39
.....

以下查询返回没有记录ID的年度最大值(在我的情况下为turid)。

SELECT year(datum)rok,max(nsp)FROM turniere GROUP BY year(datum)

结果:

rok max(nsp)
2009    41
2010    46
2011    45
2012    45
2013    42
2014    39

如何获取turid或基准值的信息?

1 个答案:

答案 0 :(得分:0)

你在中途。加入原始数据:

SELECT t.*
FROM turniere t JOIN
     (SELECT year(datum) as rok, max(nsp) as maxnsp
      FROM turniere
      GROUP BY year(datum)
     ) tt
     ON year(t.datum) = tt.rok and t.nsp = tt.maxnsp;