mysql - How to avoid the -1 in this case -


this table votes, votetype 1 = negative vote, votetype 0 = positive vote.

"id"    "votedelm"  "votetype"  "voteprocessed" "country" "3"     "6"         "1"         "0"             "us"//1-1=0  "4"     "8"         "0"         "0"             "us"//2+0-1=1 "9"     "8"         "1"         "0"             "us"  "5"     "9"         "0"         "0"             "us"//2+0-1=1 "10"    "9"         "1"         "0"             "us" 

and table likes

"id"    "type"  "parent"    "country"   "votes"     6       10      3                    1     8       10      7                    2     9       10      7                    2 

in above, run following sql count votes in votes , add or subtract them likes.

select   votedelm,   sum(case when votetype = 0 1 else -1 end) totalcount  votes  country = 'us' group votedelm; 

however, id 6 in votes, when using above query, result -1. so, 1 (in likes) minus -1 = 2. result wanted 0. also, 8 , 9 in votes table, result should have been 1 (2-1+0=1). here show 0.

can see i'm going wrong , how can right?

i think don't want show votes in negative.so.first count number of vote minus number of unlike..... try this...

 select voteby,(count(votetype)-sum(case when votetype=1 1 else 0 end)) b     t group voteby 

sql fiddle demo


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -