I want to restrict the value in the column “voteValue” to either -1 or 1, so I used an ENUM data type for this. However, when I try to calculate the total value of the votes (that is, +1 votes and -1 votes), I end up getting +2 for each -1 vote instead of subtraction.
I know I can just divide this to 2 different SQL codes to get the SUMS of +1 and -1 votes separately, but I want my code to be as simple as possible.
Any ideas?
SUM should not be used on ENUMs. Internally they are using integer values. But you should not use it for counting those numbers.
You can
group bytheenumcolumn and usecount()on other columns to get how many votes per enum is casted.See the following scenario.
Output