我希望每个Feed都有相应的rating
。
我有这个mysql
查询
SELECT
user_feed.*, COALESCE(SUM(feed_rating.rating), 0) AS rating
FROM
user_feed
LEFT JOIN
feed_rating
ON
feed_rating.feed_id = user_feed.id
但实际上我只有2 user_feeds
,它只返回一行。
如果我删除聚合函数sum,则返回两个user_feeds。
答案 0 :(得分:1)
将group by
与SUM
group by feed_id
尝试此查询
SELECT
user_feed.*, COALESCE(SUM(feed_rating.rating), 0) AS rating
FROM
user_feed
LEFT JOIN
feed_rating
ON
feed_rating.feed_id = user_feed.id group by feed_id
聚合函数对一组值执行计算 返回单个值