mysql - 如何执行查询以获取最新数据?

标签 mysql

我的表格:

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/

相关文章:

javascript - 使用 AJAX 执行通过 PHP 交付的 javascript 代码

mysql - MYSQL 和 SQLite 多用户功能有什么区别?

mysql - 获取连续一周的条目

mysql - EXISTS 行数

php - 为什么 max_allowed_pa​​cket 没有效果?

PHP_XLSXWriter_plus大型Mysql数据集缺失数据

php - 在非对象上调用成员函数 escape_string()

mysql - SQL "Truncated incorrect INTEGER value"错误

mysql - 如何检查数据库是否存在于mysql程序中

sql - MySQL创建时间和更新时间时间戳