
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to sum 3 different values in a column displaying total of each value in result set?
For this, you can use a CASE statement. Let us first create a table −
mysql> create table DemoTable ( ProductName varchar(100), ProductRating ENUM('1','2','3') ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Product-1',3); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Product-2',1); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Product-3',2); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Product-1',2); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Product-3',3); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Product-2',2); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Product-3',3); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+---------------+ | ProductName | ProductRating | +-------------+---------------+ | Product-1 | 3 | | Product-2 | 1 | | Product-3 | 2 | | Product-1 | 2 | | Product-3 | 3 | | Product-2 | 2 | | Product-3 | 3 | +-------------+---------------+ 7 rows in set (0.00 sec)
Here is the query to sum 3 different values in a column displaying a total of each value in the result set. We are adding on the basis of Product Rating −
mysql> select ProductName, sum( case when ProductRating=3 then 1 else 0 end ) as Product_3_Rating, sum( case when ProductRating=2 then 1 else 0 end ) as Product_2_Rating, sum( case when ProductRating=1 then 1 else 0 end ) as Product_1_Rating from DemoTable group by ProductName;
This will produce the following output −
+-------------+------------------+------------------+------------------+ | ProductName | Product_3_Rating | Product_2_Rating | Product_1_Rating | +-------------+------------------+------------------+------------------+ | Product-1 | 1 | 1 | 0 | | Product-2 | 0 | 1 | 1 | | Product-3 | 2 | 1 | 0 | +-------------+------------------+------------------+------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Setting column values as column names in the MySQL query result?
- MySQL query to get sum of each column where every column has same number of values?
- MySQL query to set different combinations for values in a table?
- MySQL query to avoid displaying duplicates values?
- How to set all values in a single column MySQL Query?
- Set the result of a query to a variable in MySQL?
- MySQL query to sum up values of rows and sort the result?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- How can we get only unique values of a column in MySQL result set?
- MySQL query to count the duplicate ID values and display the result in a separate column
- A single MySQL query to search multiple words from different column values
- Set user variable from result of query in MySQL?
- MySQL query to group by column and display the sum of similar values in another column
- MySQL query to find sum of fields with same column value?
- MySQL query to select the nth highest value in a column by skipping values

Advertisements