发布表2列: postid,问题
answers表4列: ansid(PK),postid,userid,answer
投票表4列: postid,userid,ansid,upvote
单个帖子的总投票
SELECT sum(votes.upvote) as 'totalupvote' FROM `answers`
JOIN post on post.postid=answers.postid
JOIN votes on votes.ansid=answers.ansid
WHERE post.postid='1'
如何计算所有帖子的投票总数?
答案 0 :(得分:2)
如果您希望每个postid
总计:
SELECT postid, sum(upvote) as sumupvote
FROM votes
GROUP BY postid
如果您还想要question
列:
select p.postid, p.question, v.sumupvote
from post p inner join (
SELECT postid, sum(upvote) as sumupvote
FROM votes
GROUP BY postid
) v on v.postid = p.postid
答案 1 :(得分:1)
显示所有帖子的投票总数
SELECT sum(votes.upvote) as 'totalupvote',post.postid,post.question FROM
votes inner JOIN post on post.postid=votes.postid
GROUP by post.postid