由于您没有提到您正在使用的数据库服务器,因此下面的查询将适用于几乎所有 RDBMS,包括MySQL
SELECT r.rating,
r.product,
l.places,
l.address,
l.telephone,
l.lng,
l.lat
FROM locations AS l
INNER JOIN ratings AS r
ON l.places = r.places
(
SELECT a.places, b.product, MAX(b.rating) max_rating
FROM locations a
INNER JOIN ratings b
ON a.places = b.places
GROUP BY a.places, b.product
) c ON l.places = c.places AND
r.product = c.product AND
r.rating = c.max_Rating
ORDER BY r.rating desc
或者,如果您的 RDBMS 支持窗口函数,
SELECT rating, product, places,
address, telephone, lng, lat
FROM
(
SELECT r.rating, r.product, l.places,
l.address, l.telephone, l.lng, l.lat
ROW_NUMBER() OVER (PARTITION BY r.product, l.places
ORDER BY r.rating DESC) rn
FROM locations AS l
INNER JOIN ratings AS r
ON l.places = r.places
) x
WHERE x.rn = 1
ORDER BY rating desc