
- 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 display the count from distinct records in the same row with MySQL?
For this, you can use GROUP_CONCAT(), COUNT() along with GROUP BY clause. Let us first create a table −
mysql> create table DemoTable -> ( -> CompanyId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> CompanyName varchar(20) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(CompanyName) values('Amazon'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(CompanyName) values('Google'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(CompanyName) values('Google'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(CompanyName) values('Microsoft'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(CompanyName) values('Amazon'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(CompanyName) values('Amazon'); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output−
+-----------+-------------+ | CompanyId | CompanyName | +-----------+-------------+ | 1 |Amazon | | 2 |Google | | 3 |Google | | 4 |Microsoft | | 5 |Amazon | | 6 |Amazon | +-----------+-------------+ 6 rows in set (0.00 sec)
Here is the query to display the count from distinct records in the same row with MySQL −
mysql> select CompanyName,group_concat(CompanyId),count(CompanyId) as count from DemoTable -> group by CompanyName -> having count > 1;
This will produce the following output −
+-------------+-------------------------+-------+ | CompanyName | group_concat(CompanyId) | count | +-------------+-------------------------+-------+ | Amazon | 1,5,6 | 3 | | Google | 2,3 | 2 | +-------------+-------------------------+-------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to display the count of distinct records from a column with duplicate records
- Display distinct dates in MySQL from a column with date records
- MySQL query to get the count of distinct records in a column
- How to implement Count (*) as variable from MySQL to display the number of records in a table?
- Display records from the current date till rest of the same month in MySQL?
- Display the count of duplicate records from a column in MySQL and order the result
- How to count the distinct column in MySQL?
- MySQL select only duplicate records from database and display the count as well?
- Display records where first and last name begins with the same letter in MySQL
- MySql how to display the records with latest ID in a table?
- How to select all the records except a row with certain id from a MySQL table?
- Count the same value of each row in a MySQL column?
- Display selected records from a MySQL table with IN() operator
- Order by a single field and display rest of the records in the same order with MySQL
- Get the first 10 rows followed by the syntax to display remaining row records with a single MySQL query

Advertisements