
- 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
MySQL Query to get count of unique values?
To count the unique values on a column, you need to use keyword DISTINCT. To understand how it is done, let us create a table. The query to create a table is as follows −
mysql> create table UniqueCountByIPAddress -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserHits int, -> UserIPAddress varchar(50), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into UniqueCountByIPAddress(UserHits,UserIPAddress) values(10,'127.0.0.1'); Query OK, 1 row affected (0.20 sec) mysql> insert into UniqueCountByIPAddress(UserHits,UserIPAddress) values(10,'127.0.0.1'); Query OK, 1 row affected (0.20 sec) mysql> insert into UniqueCountByIPAddress(UserHits,UserIPAddress) values(20,'127.0.0.1'); Query OK, 1 row affected (0.20 sec) mysql> insert into UniqueCountByIPAddress(UserHits,UserIPAddress) values(20,'127.0.0.1'); Query OK, 1 row affected (0.17 sec) mysql> insert into UniqueCountByIPAddress(UserHits,UserIPAddress) values(20,'127.0.0.2'); Query OK, 1 row affected (0.13 sec) mysql> insert into UniqueCountByIPAddress(UserHits,UserIPAddress) values(20,'127.0.0.2'); Query OK, 1 row affected (0.20 sec) mysql> insert into UniqueCountByIPAddress(UserHits,UserIPAddress) values(30,'127.0.0.2'); Query OK, 1 row affected (0.17 sec) mysql> insert into UniqueCountByIPAddress(UserHits,UserIPAddress) values(30,'127.0.0.2'); Query OK, 1 row affected (0.28 sec) mysql> insert into UniqueCountByIPAddress(UserHits,UserIPAddress) values(30,'127.0.0.2'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from UniqueCountByIPAddress;
The following is the output −
+----+----------+--------------+ | Id | UserHits | UserIPAddress| +----+----------+--------------+ | 1 | 10 | 127.0.0.1 | | 2 | 10 | 127.0.0.1 | | 3 | 20 | 127.0.0.1 | | 4 | 20 | 127.0.0.1 | | 5 | 20 | 127.0.0.2 | | 6 | 20 | 127.0.0.2 | | 7 | 30 | 127.0.0.2 | | 8 | 30 | 127.0.0.2 | | 9 | 30 | 127.0.0.2 | +----+----------+--------------+
The following is the query to count the distinct value on column ‘UserIPAddress’ −
mysql> select UserHits,count(distinct UserIPAddress) as TotalHits -> from UniqueCountByIPAddress group by UserHits;
The following is the output −
+----------+-----------+ | UserHits | TotalHits | +----------+-----------+ | 10 | 1 | | 20 | 2 | | 30 | 1 | +----------+-----------+ 3 rows in set (0.05 sec)
- Related Articles
- Get multiple count in a single MySQL query for specific column values
- MySQL query to get the count of rows in which two or more specified values appear?\n
- Get the count of only unique rows in a MySQL column?
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- MySQL query to count number of duplicate values in a table column
- How to merge queries in a single MySQL query to get the count of different values in different columns?
- Python - Unique values count of each Key
- MySQL query to get the count of distinct records in a column
- Count boolean field values within a single MySQL query?
- MySQL query to get the count of all the elements in the field?
- Get distinct values and count them in MySQL
- MySQL query to count where more than three columns values are true?
- MySQL query to return the count of only NO values from corresponding column value
- MySQL query to group results by date and display the count of duplicate values?
- How to use a single MySQL query to count column values ignoring null?

Advertisements