所以我有一个名为cities的表,其中包含属性路径(可以有重复或更多相等的条目),name和kms,我需要对其进行过滤,以便我只有一个条目,每条条目最多的路径公里。我现在拥有的是
SELECT cities.*
FROM cities, categories
group by cities.path , cities.kms
ORDER BY cities.kms desc
问题是它给了我重复的路径条目(我不想要),而且它也没有像我想要的那样按kms命令。我该怎么办?
答案 0 :(得分:3)
我认为查询中categories
表没有任何意图,所以我在答案中将其删除了。
SELECT c1.path, c1.name, c1.kms
FROM cities c1
INNER JOIN (SELECT c2.path, MAX(c2.kms) AS Maxkms
FROM cities c2
GROUP BY c2.path) q
ON c1.path = q.path
AND c1.kms = q.Maxkms
答案 1 :(得分:1)
GROUP BY path
仅限于MAX(kms)
:
SELECT cities.path, MAX(cities.kms)
FROM cities
GROUP BY cities.path
ORDER BY cities.kms desc
ORDER BY
应按kms
排序,除非它不是数字类型,可能是字符串。确保其类型。