myTable的:
DATE NAME POCKET1 POCKET2 LOCATION 1/1/2010 john 5 30 home 2/1/2010 John 4 10 school 3/1/2010 john 2 20 mall
我需要帮助来查询myTable
,以获得预期结果。
我知道如何获得最高和最高平均,但不知道如何获得最新的LOCATION
。
select NAME,max(POCKET1),avg(POCKET2) from myTable
where NAME = 'john' group by NAME;
预期结果:
NAME max(POCKET1) avg(POCKET2) LatestLOCATION john 5 20 mall
答案 0 :(得分:1)
按日期desc
获得前1名select
NAME
,max(POCKET1)
,avg(POCKET2)
, (select location from mytable order by date desc limit 1) as Location
from myTable
where NAME = 'john' group by NAME;
如果您想获得John的最新位置
select
NAME
,max(POCKET1)
,avg(POCKET2)
, (select location from mytable y where y.name = x.name order by date desc limit 1) as Location
from myTable x
where NAME = 'john' group by NAME;