
- 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
Count multiple rows and display the result in different columns (and a single row) with MySQL
Let us first create a table −
mysql> create table DemoTable1452 -> ( -> FavouriteColor varchar(50) -> ); Query OK, 0 rows affected (2.42 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1452 values('Red'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1452 values('Yellow'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1452 values('Yellow'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1452 values('Yellow'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1452 values('Blue'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable1452 values('Blue'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable1452 values('Red'); Query OK, 1 row affected (0.29 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1452;
This will produce the following output −
+----------------+ | FavouriteColor | +----------------+ | Red | | Yellow | | Yellow | | Yellow | | Blue | | Blue | | Red | +----------------+ 7 rows in set (0.00 sec)
Following is the query to count multiple rows and display the result in different columns −
mysql> select sum(FavouriteColor='Red') as NumberOfRedColor, -> sum(FavouriteColor='Yellow') as NumberOfYellowColor, -> sum(FavouriteColor='Blue') as NumberOfBlueColor -> from DemoTable1452;
This will produce the following output −
+------------------+---------------------+-------------------+ | NumberOfRedColor | NumberOfYellowColor | NumberOfBlueColor | +------------------+---------------------+-------------------+ | 2 | 3 | 2 | +------------------+---------------------+-------------------+ 1 row in set (0.27 sec)
- Related Articles
- Concatenate multiple rows and columns in a single row with MySQL
- Count values based on conditions and display the result in different columns with MySQL?
- Select multiple columns and display in a single column in MySQL?
- Display substrings of different length with a single MySQL query and combine the result
- Update multiple columns of a single row MySQL?
- Count and sort rows with a single MySQL query
- Select distinct names from two columns in MySQL and display the result in a single column
- Get values from all rows and display it a single row separated by comma with MySQL
- MySQL multiple COUNT with multiple columns?
- Count two different columns in a single query in MySQL?
- Count duplicate ids and display the result in a separate column with MySQL
- MySQL query to get the length of all columns and display the result in a single new column?
- Divide numbers from two columns and display result in a new column with MySQL
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- Select multiple sums with MySQL query and display them in separate columns?

Advertisements