
- 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
Can we ignore duplicate rows in COUNT?
Yes, we can ignore duplicate rows in COUNT using DISTINCT. Following is the syntax:
select count(distinct yourColumnName) from yourTableName;
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
- How can we ignore the fields during JSON serialization in Java?
- How can we use MySQL SELECT statement to count number of rows in a table?
- How to count the number of duplicate rows in an R data frame?
- How can we write PHP script to count the affected rows by MySQL query?
- Ignore MySQL INSERT IGNORE statement and display an error if duplicate values are inserted in a table
- How can we ignore the negative values return by MySQL DATEDIFF() function?
- Finding Duplicate Rows in MySQL (Composite Key)?
- How do we count the total duplicate records in a column of MySQL table?
- How to avoid inserting duplicate rows in MySQL?
- How to prevent duplicate rows in MySQL INSERT?
- How to drop duplicate rows in pandas series?
- How to find the count of duplicate rows if they are greater than n in R data frame?
- How can we detect duplicate labels using the Python Pandas library?
- Can we get total number of rows in a MySQL database?
- Find duplicate rows in a binary matrix in C++

Advertisements