我的表格:
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
I need help to query myTable
, to get as in the expected result.
I know how to get the max & avg, but do not know how to get the latest 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
最佳答案
按日期描述获取前 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;
如果你想得到约翰完成的最新位置
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;
关于mysql - 如何执行查询以获取最新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44275207/