我有两张表格
comments
------------------
id text
rates
---------------------
id comment_id score
我的问题是...但sum操作不受影响,只返回一行。
SELECT *, SUM(rates.score) FROM comment LEFT JOIN rate ON (rate.comment_id = comment.id)
我有两张表格
comments
------------------
id text
rates
---------------------
id comment_id score
我的问题是...但sum操作不受影响,只返回一行。
SELECT *, SUM(rates.score) FROM comment LEFT JOIN rate ON (rate.comment_id = comment.id)
SELECT comment.*, SUM(rates.score) FROM comment LEFT JOIN rate ON (rate.comment_id = comment.id)
GROUP BY comment.id
SELECT a.*, SUM(b.score) AS ratingsum
FROM comment a
LEFT JOIN rate b ON a.id = b.comment_id
GROUP BY a.id
尝试添加一个group by子句:
SELECT comment.*, SUM(rates.score) FROM comment LEFT JOIN rate ON rate.comment_id = comment.id GROUP BY rates.comment_id;