- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I count unique records from a column in MySQL database?
For this, use aggregate function count(*) to count to GROUP BY to group. Let us first create a table −
mysql> create table DemoTable ( UserName varchar(100), UserPostMessage text ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','Hi'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('David','Hello'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Chris','Awesome'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Chris','Amazing'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('David','Nice Place'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Chris','Amazing'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------+-----------------+ | UserName | UserPostMessage | +----------+-----------------+ | Chris | Hi | | David | Hello | | Chris | Awesome | | Chris | Amazing | | David | Nice Place | | Chris | Amazing | +----------+-----------------+ 6 rows in set (0.00 sec)
Here is the query to count unique records from a column in my MySQL database −
mysql> select UserName,count(DISTINCT UserPostMessage) AS NumberOfMessagesPostByUser from DemoTable group by UserName;
This will produce the following output −
+----------+----------------------------+ | UserName | NumberOfMessagesPostByUser | +----------+----------------------------+ | Chris | 3 | | David | 2 | +----------+----------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- How can we count a number of unique values in a column in MySQL table?
- How can I check MySQL tables from a database in accordance with particular column/s name?
- Given a column name how can I find which tables in a MySQL database contain that column?
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL select only duplicate records from database and display the count as well?
- Get the count of only unique rows in a MySQL column?
- How can I drop an existing column from a MySQL table?
- Display the count of duplicate records from a column in MySQL and order the result
- How do we count the records from MySQL table where column holds duplicate/triplicates data?
- Can I get the count of repeated values in a column with MySQL?
- Can I search for particular numbers in a MySQL column with comma separated records using a MySQL query?
- How do we count the total duplicate records in a column of MySQL table?
- How can I get the records from MySQL table in result set in a particular way?
- MySQL query to get the count of distinct records in a column
- How can I get enum possible values in a MySQL database?

Advertisements