我有一个表,其中包含类似于以下的数据。我需要的查询将导致具有最高 cityID 值的 Orange 和 Apple 的最佳结果。
id | fruit | attr | cityID | personID
163 Apple green 3685 235
163 Apple red 3145 267
163 Apple yellow 1522 560
164 Orange big 1344 147
164 Orange small 833 2673
我需要的结果是
id | fruit | attr | cityID | personID
163 Apple green 3685 235
164 Orange big 1344 147
我开始尝试用
来完成这个select
fruit_id,
fruit,
attr,
max(cityID),
personID
from fruits_cities
group by
fruit_id,
fruit,
attr,
max(cityID),
personID
最佳答案
这是一个挑战。您需要使用某种比较。这是一个简单的方法:
select fc.*
from fruits_cities fc
where fc.cityId = (select max(CityId)
from fruits_cities fc2
where fc2.fruit_id = fc.fruit_id
);
关于mysql - 仅选择具有其他唯一列的特定记录的最高值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32803954/