
- 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 the number of distinct values in MySQL?
Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(100), -> Code varchar(100) -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','0001'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Robert','0002'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Robert','0003'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris','0001'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+--------+------+ | Name | Code | +--------+------+ | Chris | 0001 | | Robert | 0002 | | Robert | 0003 | | Chris | 0001 | +--------+------+ 4 rows in set (0.00 sec)
Following is the query to count the number of distinct values −
mysql> select Name,count(distinct Code) from DemoTable group by Name;
Output
+--------+----------------------+ | Name | count(distinct Code) | +--------+----------------------+ | Chris | 1 | | Robert | 2 | +--------+----------------------+ 2 rows in set (0.06 sec)
- Related Articles
- Count occurrences of known distinct values in MySQL
- How to count distinct values in MySQL?
- Get distinct values and count them in MySQL
- Get the maximum count of distinct values in a separate column with MySQL
- How to return distinct values in MySQL and their count?
- How to count number of distinct values per field/ key in MongoDB?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- How to count the distinct column in MySQL?
- MySQL SELECT DISTINCT and count?
- MySQL Count Distinct values process is very slow. How to fasten it?
- Count number of Distinct Substring in a String in C++
- MySQL query to count number of duplicate values in a table column
- MySQL query to get the count of distinct records in a column
- Count distinct points visited on the number line in C++
- Program to count number of distinct substrings in s in Python

Advertisements