Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-08-25 15:57:09
forward
1617 people have browsed it

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;
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!