我的问题类比是这样的:
table: place
id title branch
===============================
1 pizza store california
2 pizza store manhattan
3 coffee shop california
4 pizza store texas
5 cookie store new york
table: rating
id place_id rating
================================
1 1 3.5
2 2 5.0
3 2 4.2
4 2 5.0
5 5 4.0
我使用查询:
SELECT F.id AS id, F.title AS title, G.rating
FROM place F
JOIN rating G ON F.id = G.place_id
GROUP BY F.title
显示屏将是:
id title rating
===========================
1 pizza store 3.5
3 coffee shop -
5 cookie store 4.0
我想要的是对具有最大值的评级进行排序,在这种情况下,我希望它显示评级为5且id为2的披萨店。是否可以在GROUP BY中插入一些子查询?
提前致谢!!
答案 0 :(得分:3)
试
SELECT F.id , F.title, s.Maxrating
FROM place F
INNER JOIN (SELECT id,Max(rating) as MaxRating FROM rating GROUP BY place_id ) s
ON s.id = F.id
答案 1 :(得分:2)
SELECT F.id AS id, F.title AS title, max(G.rating)
FROM place F, rating G
where F.id = G.place_id(+)
group by f.id, f.title