
- 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
Get the count of only unique rows in a MySQL column?
In MySQL, COUNT() will display the number of rows. DISTINCT is used to ignore duplicate rows and get the count of only unique rows.
Let us first create a table:
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(10) ); Query OK, 0 rows affected (0.47 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable(FirstName) values('Larry'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName) values('Sam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(FirstName) values('Larry'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(FirstName) values('Carol'); Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(FirstName) values('Mike'); Query OK, 1 row affected (0.12 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output:
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Larry | | 2 | John | | 3 | Sam | | 4 | John | | 5 | John | | 6 | Larry | | 7 | Mike | | 8 | Robert | | 9 | Carol | | 10 | Mike | +----+-----------+ 10 rows in set (0.00 sec)
Following is the query to ignore duplicate rows in COUNT and get the count of only unique rows:
mysql> select count(distinct FirstName) from DemoTable;
This will produce the following output:
+---------------------------+ | count(distinct FirstName) | +---------------------------+ | 6 | +---------------------------+ 1 row in set (0.03 sec)
- Related Articles
- Get count of values that only appear once in a MySQL column?
- How can we get only unique values of a column in MySQL result set?
- Get the sum of a column in all MySQL rows?
- MySQL Query to get count of unique values?
- Get the count of unique phone numbers from a column with phone numbers declared as BIGINT type in MySQL
- MySQL query to count rows with a specific column?
- Get the count of duplicate values from a single column in MySQL?
- MySQL query to get the count of distinct records in a column
- How can I count unique records from a column in MySQL database?
- How can we count a number of unique values in a column in MySQL table?
- How can we get all the unique rows in MySQL result set?
- Selecting and displaying only some rows from a column in a MySQL table
- Get the maximum count of distinct values in a separate column with MySQL
- Can I get the count of repeated values in a column with MySQL?
- How to get the count of a specific value in a column with MySQL?

Advertisements