MySQL时间范围内的MAX最大值并分组

原始数据如下图。
原始数据样例.jpg
期望获得如下结果,也就是过去三天,每个国家、省份、城市(其中城市的名字有重复,比如城市名字是zhangsan,属于lisi省,同样有可能属于wangwu省)中temprature最大的那一行
期望结果.jpg

如下SQL可以获得想要的数据,但是获取不到ID以及对应的日期(date字段),需求是想把对应行的所有字段获取到,先提前谢谢啦!
mysql>select country,province,city,max(temprature) as maxtemprature from table1 where date >= '2020-06-18' and date <= '2020-06-20' group by country,province,city

country province city maxtemprature
china guangdong guangzhou 39
china zhejiang hangzhou 37
china jiangsu nanjing 39

阅读 3.8k
1 个回答

假设 china guangdong guangzhou 有 2天 都是 39度,那么取哪个 ID 呢?

无所谓的话
right join table t2 on t2.country=t1.country and t2.province=t1.province and t2.city=t1.city and t2.temprature=t1.temprature

left join 可能会有多条记录,所以要用 right join

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题