Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql sum using one to many relations

i have two tables

comments
------------------
id       text

rates
---------------------
id    comment_id    score

My query is ... but sum operation not affected. just one row will be returen.

SELECT *, SUM(rates.score) FROM comment LEFT JOIN rate ON (rate.comment_id = comment.id)
like image 443
sweb Avatar asked Dec 06 '25 18:12

sweb


1 Answers

SELECT comment.*, SUM(rates.score) FROM comment LEFT JOIN rate ON (rate.comment_id = comment.id)
GROUP BY comment.id
like image 182
vearutop Avatar answered Dec 08 '25 07:12

vearutop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!