Get the count of unique phone numbers from a column with phone numbers declared as BIGINT type in MySQL

MySQLMySQLi Database

For this, you can use COUNT() along with DISTINCT. The COUNT() method is to count the records. However, the DISTINCT returns distinct records, whereas COUNT() method counts those unique records. Let us first create a table −

mysql> create table DemoTable
(
   PhoneNumber bigint
);
Query OK, 0 rows affected (1.29 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(8567789898);
Query OK, 1 row affected (0.94 sec)
mysql> insert into DemoTable values(8567789898);
Query OK, 1 row affected (0.34 sec)
mysql> insert into DemoTable values(9876564534);
Query OK, 1 row affected (0.43 sec)
mysql> insert into DemoTable values(9087896545);
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable values(7656456589);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(9876564534);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(9087896545);
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+
| PhoneNumber |
+-------------+
| 8567789898  |
| 8567789898  |
| 9876564534  |
| 9087896545  |
| 7656456589  |
| 9876564534  |
| 9087896545  |
+-------------+
7 rows in set (0.00 sec)

Following is the query to count unique phone numbers from a column with phone numbers −

mysql> select count(distinct PhoneNumber) AS UniquePhoneNumber from DemoTable;

This will produce the following output −

+-------------------+
| UniquePhoneNumber |
+-------------------+
|                 4 |
+-------------------+
1 row in set (0.00 sec)
raja
Published on 03-Oct-2019 06:03:54
Advertisements