1

我的问题类比是这样的:

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 中插入一些子查询?

提前致谢!!

4

2 回答 2

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 
于 2012-04-11T04:35:55.087 回答
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
于 2012-04-11T04:42:30.633 回答