How do you use distinct and count in MySQL?

In MySQL, DISTINCT and COUNT serve different purposes, with DISTINCT used to remove duplicates and COUNT used to calculate the number of rows.

  1. Remove duplicate values using DISTINCT:
SELECT DISTINCT column_name FROM table_name;

This statement will select the unique values from a specified column in the table and remove duplicate values.

  1. Calculate the number of rows using COUNT.
SELECT COUNT(column_name) FROM table_name;

This statement will calculate the number of non-empty values in the specified column.

注意:The COUNT function can also be used in combination with DISTINCT to calculate the number of unique values in a specified column.

SELECT COUNT(DISTINCT column_name) FROM table_name;
Leave a Reply 0

Your email address will not be published. Required fields are marked *