
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- Sum a negative number (negative and positive digits) - JavaScript
- Java Program to convert positive int to negative and negative to positive
- Reversing negative and positive numbers in JavaScript
- Positive Testing and Negative Testing with Examples
- Positive, negative and zeroes contribution of an array in JavaScript
- Converting boolean values to positive or negative sign in MySQL?
- Counting the clusters of positive numbers - JavaScript Arrays
- How to find the groupwise number of positive and negative values in an R data frame?
- What are the positive and negative impacts of a working mother?
- 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?
- Lambda expression in Python to rearrange positive and negative numbers
- How to print positive and negative infinity values in JavaScript?
- Python - Sum negative and positive values using GroupBy in Pandas
- Display the Numerical positive and negative element-wise in Numpy
Advertisements