- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Counting number of positive and negative votes in MySQL?
To count number of positive and negative votes, you can use CASE statement along with aggregate function SUM().
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Vote int ); Query OK, 0 rows affected (1.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Vote) values(-10); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Vote) values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Vote) values(45); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Vote) values(-6); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Vote) values(-1000); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Vote) values(450); Query OK, 1 row affected (0.14 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------+ | Id | Vote | +----+-------+ | 1 | -10 | | 2 | 100 | | 3 | 45 | | 4 | -6 | | 5 | -1000 | | 6 | 450 | +----+-------+ 6 rows in set (0.00 sec)
Following is the query to count number of positive and negative votes −
mysql> select sum(case when Vote > 0 then 1 else 0 end) as Number_Of_Positive_Votes, sum(case when Vote < 0 then 1 else 0 end) as Number_Of_Negative_Votes from DemoTable;
This will produce the following output −
+--------------------------+--------------------------+ | Number_Of_Positive_Votes | Number_Of_Negative_Votes | +--------------------------+--------------------------+ | 3 | 3 | +--------------------------+--------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Sum a negative number (negative and positive digits) - JavaScript
- Why is there no zero positive and negative number?
- Java Program to convert positive int to negative and negative to positive
- How Addition Of Positive Number Is Similar To Subtraction Of Negative Number
- Distinguish positive and negative numbers.
- Schizophrenia Symptoms: Positive and Negative?
- In an election, there were only two candidates. The winner polled 53% votes and won by 9600 votes. Find the total number of votes polled
- Reversing negative and positive numbers in JavaScript
- Positive and Negative Logic in Digital Electronics
- Converting boolean values to positive or negative sign in MySQL?
- Explain the products of positive and negative signs.
- Positive, negative and zeroes contribution of an array in JavaScript
- Find count of positive and negative array elements in Java
- Display the sum of positive and negative values from a column in separate columns with MySQL
- How to convert positive value to negative while inserting in MySQL?

Advertisements