Home >Database >Mysql Tutorial >How to Count Unique Values in a Specific Database Table Column?
Counting Unique Values in a Database Table
When working with tables containing multiple columns, it often becomes necessary to determine the number of unique values within a specific column. Considering the example provided with three columns: orderNumber, name, and email, the goal is to count how many unique emails exist in the table.
Using the standard SELECT count(email) query retrieves the total number of emails, including duplicates. To count the number of unique emails, it is essential to exclude any duplicate values. However, the statement SELECT DISTINCT count(email) does not produce the expected result.
The correct syntax for counting unique values involves using the DISTINCT keyword within the parentheses, as shown below:
SELECT count(DISTINCT(email)) FROM orders
By enclosing the email column within the DISTINCT parentheses, the query ensures that only unique values are counted. This approach accurately provides the number of unique emails in the orders table, excluding any duplicates that might exist.
The above is the detailed content of How to Count Unique Values in a Specific Database Table Column?. For more information, please follow other related articles on the PHP Chinese website!