Database table:
+----------------------------------------------+
| vote_id | user_id | vote_type | fieldset_id |
+----------------------------------------------+
| 1 | 1 | up | 111 |
| 2 | 2 | up | 111 |
| 3 | 3 | down | 111 |
| 4 | 4 | up | 111 |
+----------------------------------------------+
Mysql查询:
select vote_type,count(vote_type) as total_vote
from vote_mst
where fieldset_id='111'
group by vote_type
预期输出: +3
我正在接受
+-------------------------+
| vote_type | total_vote |
+-------------------------+
| up | 3 |
| down | 1 |
+-------------------------+
在MysQL查询中计算(Upvote-Downvote)
答案 0 :(得分:0)
我想你想要这样的东西:
select sum(vote_type = 'up') - sum(vote_type = 'down') as net_votes,
count(*) as total_votes
from vote_mst
where fieldset_id = '111' ;
答案 1 :(得分:0)
试试这个:
select `fieldset_id`,`vote_type`,SUM(IF(`vote_type`='up',1,-1)) as `total_vote`
from `vote_mst`
GROUP BY `fieldset_id`