MySQL狡猾的选择

时间:2012-04-20 11:33:17

标签: mysql greatest-n-per-group

无法理解如何选择: 表结构:

id name value date
1 ivan    2    2010-01-01
2 ivan    3    2010-05-08
3 ivan    1    2009-04-14
4 sasha   2    2011-11-11
5 sasha   9    2012-04-04

如何获得?:

name value(in last time)
ivan  3
sasha 9

4 个答案:

答案 0 :(得分:1)

这是未经测试的,但应该有效:

SELECT
    name,
    MAX(value)
FROM
    structure
GROUP BY
    name

答案 1 :(得分:0)

SELECT DISTINCT name, value FROM table ORDER BY date DESC

这就是我目前对你目前描述的理解。

答案 2 :(得分:0)

这样的事情:

SELECT
    MAX(value),
    name
FROM
    Table
GROUP BY
    name

答案 3 :(得分:0)

此查询应该有效

select f.name, f.value, f.date
from (
   select id,name,max(date) as dat from tableName group by name
) as x inner join tableName as f on f.name = x.name and f.date = x.dat;