
- 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 get the count of each distinct value in a column in MySQL?
Let us see an example to get the count of each distinct value in a column. Firstly, we will create a table.
The CREATE command is used to create a table.
mysql> create table DistinctDemo1 - > ( - > id int, - > name varchar(100) - > ); Query OK, 0 rows affected (0.43 sec)
Inserting records
mysql> insert into DistinctDemo1 values(1,'John'); Query OK, 1 row affected (0.34 sec) mysql> insert into DistinctDemo1 values(2,'John'); Query OK, 1 row affected (0.20 sec) mysql> insert into DistinctDemo1 values(3,'John'); Query OK, 1 row affected (0.09 sec) mysql> insert into DistinctDemo1 values(4,'Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DistinctDemo1 values(5,'David'); Query OK, 1 row affected (0.12 sec)
Displaying all records
mysql> select *from DistinctDemo1;
The following is the output that displays all the records.
+------+-------+ | id | name | +------+-------+ | 1 | John | | 2 | John | | 3 | John | | 4 | Carol | | 5 | David | +------+-------+ 5 rows in set (0.00 sec)
The following is the syntax to get the count.
mysql> SELECT name,COUNT(1) as OccurenceValue FROM DistinctDemo1 GROUP BY name ORDER BY OccurenceValue;
Here is the output.
+-------+----------------+ | name | OccurenceValue | +-------+----------------+ | Carol | 1 | | David | 1 | | John | 3 | +-------+----------------+ 3 rows in set (0.04 sec)
- Related Articles
- MySQL query to get the count of distinct records in a column
- How to count the distinct column in MySQL?
- Get the maximum count of distinct values in a separate column with MySQL
- How to get the count of a specific value in a column with MySQL?
- Count the same value of each row in a MySQL column?
- How to get the sum for every distinct value in another column in MySQL?
- Get distinct values and count them in MySQL
- How to count distinct values in MySQL?
- Is it possible to combine 'DISTINCT' and 'COUNT' queries, so that I can see how many times each distinct value appears in a MySQL table column?
- Averaging a total from a Score column in MySQL with the count of distinct ids?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Get the count of only unique rows in a MySQL column?
- How to select distinct value from one MySQL column only?
- How to get the seed value of an identity column in MySQL?
- Get count of how many times a string appears in a MySQL column?

Advertisements