A2-02-02.DML-Using MySQL DISTINCT to Eliminate Duplicates

转载自:http://www.mysqltutorial.org/mysql-distinct.aspx

 

Home Basic MySQL Tutorial / Using MySQL DISTINCT to Eliminate Duplicates

Using MySQL DISTINCT to Eliminate Duplicates

 

Summary: in this tutorial, you will learn how to use MySQL DISTINCT clause with the SELECT statement to eliminate duplicate rows in a result set.

Introduction to MySQL DISTINCT clause

When querying data from a table, you may get duplicate rows. In order to remove these duplicate rows, you use the DISTINCT clause in the SELECT statement.

The syntax of using the DISTINCT clause is as follows:

 

MySQL DISTINCT example

Let’s take a look a simple example of using the DISTINCT clause to select the unique last names of employees from the employees table.

First, we query the last names of employees from the employees table using the SELECT statement as follows:

Try It Out

 

mysql-duplicate-last-name

Some employees have the same last name  Bondur,Firrelli etc.

To remove the duplicate last names, you add the DISTINCT clause to the SELECT statement as follows:

Try It Out

MySQL DISTINCT last name
The duplicate last names are eliminated in the result set when we used the DISTINCT clause.

MySQL DISTINCT and NULL values

If a column has NULL values and you use the DISTINCT clause for that column, MySQL keeps one NULLvalue and eliminates the other because the DISTINCT clause treats all NULL values as the same value.

For example, in the customers table, we have many rows whose state column has NULL values. When we use the DISTINCT clause to query the customers’ states, we will see unique states and a NULL value as the following query:

Try It Out

MySQL DISTINCT with NULL example

MySQL DISTINCT with multiple columns

You can use the DISTINCT clause with more than one column. In this case, MySQL uses the combination of all columns to determine the uniqueness of the row in the result set.

For example, to get the unique combination of city and state from the customers table, you use the following query:

Try It Out

MySQL DISTINCT multiple columns example

Without the DISTINCT clause, you will get the duplicate combination of state and city as follows:

Try It Out

MySQL without DISTINCT clause on multiple columns

DISTINCT clause vs. GROUP BY clause

If you use the GROUP BY clause in the SELECT statement without using aggregate functions, the GROUP BY clause behaves like the DISTINCT clause.

The following statement uses the GROUP BY clause to select the unique states of customers from the customers table.

Try It Out

MySQL DISTINCT vs GROUP BY example

You can achieve the similar result by using the DISTINCT clause:

Try It Out

MySQL DISTINCT vs GROUP BY example with sorting

Generally speaking, the DISTINCT clause is a special case of the GROUP BY clause. The difference between DISTINCT clause and GROUP BY clause is that the GROUP BY clause sorts the result set whereas the DISTINCT clause does not.

If you add the ORDER BY clause to the statement that uses the  DISTINCT clause, the result set is sorted and it is the same as the one returned by the statement that uses GROUP BY clause.

Try It Out

MySQL DISTINCT and aggregate function

You can use the DISTINCT clause with an aggregate function e.g., SUMAVG, and COUNT, to remove duplicate rows before MySQL applies the aggregate function to the result set.

For example, to count the unique states of customers in the U.S., you use the following query:

Try It Out

MySQL DISTINCT with COUNT function

MySQL DISTINCT with LIMIT clause

In case you use the DISTINCT clause with the LIMIT clause, MySQL stops searching immediately when it finds the number of unique rows specified in the LIMIT clause.

The following query selects the first 5 non-null unique states in the customers table.

Try It Out

MySQL DISTINCT with LIMIT clause

In this tutorial, we have shown you various ways of using MySQL DISTINCT clause such as eliminating duplicate rows and counting non-NULL values.

posted @ 2018-08-21 14:55  zhuntidaoren  阅读(164)  评论(0编辑  收藏  举报