[英]where clause sub query using avg
我有2張桌子,食物和分數。
餐飲
id
name
picture
得分
id
score
food_id
。 我想查詢平均得分高於7的食物:
select * from food where (select avg(score) from score group by food_id)>=7
但這又回來了
錯誤#1242-子查詢返回的行數超過1。
嘗試這個
SELECT * FROM food f INNER JOIN score s
ON f.id = s.food_id
WHERE avg(s.score)>=7
GROUP BY f.food_id
試試這個:
select fd.* from food fd
inner join score sc on fd.id=sc .food_id
group by food.id
having avg(sc.score)>=7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.