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;
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!