我在MySQL中有一个表,其中包含以下列
表 - 评论
Review_Id
Business_Id
Title
Description
Useful_Count
特定的business_id(外键)可以有很多评论。我想要的是在给定一组业务ID
的情况下从DB中提取此信息是否可以使用单个SQL查询获取这两种信息?
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个:
SELECT r.Business_Id, t.c AS 'Number of Reviews',
r.Title, r.Description, t.max_useful_count
FROM Reviews r
JOIN (SELECT Business_Id, COUNT(Business_Id) AS c,
MAX(Useful_Count) AS max_useful_count
FROM Reviews
WHERE r.Business_Id IN (...)
GROUP BY Business_Id) as t
ON r.Business_Id = t.Business_Id
AND r.Useful_Count = t.max_useful_count