Home >Database >Mysql Tutorial >How can we count the number of unique values ​​in a column in a MySQL table?

How can we count the number of unique values ​​in a column in a MySQL table?

WBOY
WBOYforward
2023-08-25 15:57:091681browse

How can we count the number of unique values ​​in a column in a MySQL table?

We can count the number of unique values ​​in a column by using the DISTINCT keyword and the column name as arguments to the COUNT() function. The syntax is as follows -

SELECT COUNT(DISTINCT Col_name) FROM table_name;

Example

Suppose we have the following table

mysql> Select * from tender;
+----------+--------------+--------------+-------+
| clientid | client_Fname | Client_Lname | value |
+----------+--------------+--------------+-------+
|   100    | Mohan        | Kumar        | 60000 |
|   101    | Sohan        | Singh        | 50000 |
|   101    | Somil        | Rattan       | 55000 |
|   103    | Gaurav       | Kumar        | 75000 |
|   103    | Rahul        | Singh        | 63000 |
+----------+--------------+--------------+-------+
5 rows in set (0.00 sec)

Now, if we want to count the total number of unique values ​​in the column named "clientid", we can do it with the help of the following query -

mysql> Select COUNT(DISTINCT Clientid) from tender;
+--------------------------+
| COUNT(DISTINCT Clientid) |
+--------------------------+
|                        3 |
+--------------------------+
1 row in set (0.00 sec)

If we want to count the total number of unique values ​​in the column named "Client_Lname", we can do it with the help of the following query -

mysql> Select COUNT(DISTINCT Client_Lname) from tender;
+------------------------------+
| COUNT(DISTINCT Client_Lname) |
+------------------------------+
|                            3 |
+------------------------------+
1 row in set (0.00 sec)

The above is the detailed content of How can we count the number of unique values ​​in a column in a MySQL table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete