
- 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
How to combine few row records in MySQL?
For this, use CASE WHEN concept. Let us first create a table −
mysql> create table demo68 −> ( −> id int not null auto_increment primary key, −> company_name varchar(50), −> employee_name varchar(50), −> country_name varchar(50) −> ); Query OK, 0 rows affected (1.86 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo68(company_name,employee_name,country_name) values('Google','John','US'); Query OK, 1 row affected (0.29 sec) mysql> insert into demo68(company_name,employee_name,country_name) values('Google','Bob','UK'); Query OK, 1 row affected (0.10 sec) mysql> insert into demo68(company_name,employee_name,country_name) values('Google','David','AUS'); Query OK, 1 row affected (0.08 sec)
Display records from the table using select statement −
mysql> select *from demo68;
This will produce the following output −
+----+--------------+---------------+--------------+ | id | company_name | employee_name | country_name | +----+--------------+---------------+--------------+ | 1 | Google | John | US | | 2 | Google | Bob | UK | | 3 | Google | David | AUS | +----+--------------+---------------+--------------+ 3 rows in set (0.00 sec)
Following is the query to combine few row records in MySQL −
mysql> select −> company_name, −> max(case when country_name= 'US' then employee_name end) as US_Employee_Name, −> max(case when country_name= 'UK' then employee_name end) as UK_Employee_Name, −> max(case when country_name= 'AUS' then employee_name end) as AUS_Employee_Name −> from demo68 −> group by company_name;
This will produce the following output −
+--------------+------------------+------------------+-------------------+ | company_name | US_Employee_Name | UK_Employee_Name | AUS_Employee_Name | +--------------+------------------+------------------+-------------------+ | Google | John | Bob | David | +--------------+------------------+------------------+-------------------+ 1 row in set (0.05 sec)
- Related Articles
- Combine multiple text records to one in MySQL
- How can we combine ROW selection with COLUMN selection in MySQL?
- How to combine two tables and add a new column with records in MySQL?
- How to display the count from distinct records in the same row with MySQL?
- MySQL query to return multiple row records with AND & OR operator
- Fetch datetime row from exactly past 7 days records in MySQL
- How to select all the records except a row with certain id from a MySQL table?
- How can we combine functions in MySQL?
- How to select last row in MySQL?
- How to sum current month records in MySQL?
- How to ignore specific records and add remaining corresponding records (numbers) in MySQL?
- How to select next row pagination in MySQL?
- In SAP ABAP, few records are skipped during parallel processing
- How to display records vertically in MySQL command line?
- How to update a range of records in MySQL?

Advertisements