zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    Asp.Net Core Web MVC 调用Grpc,采用依赖注入
    .Net Core框架下 Grpc四种处理方法
    信息系统项目管理师高频考点(第二章)
    系统集成项目管理工程师高频考点(第二章)
    .Net Core框架下实现Grpc客户端和服务端
    .Net Framework框架下实现Grpc客户端和服务端
    Asp.Net Core Mvc项目登录IdentityServer4验证无法跳转问题
    IdentityServer4(五)
    MVC项目登录IdentityServer4报错, The cookie '.AspNetCore.Correlation has set 'SameSite=None' and must also set 'Secure'
    IdentityServer4(二)
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/9511358.html
Copyright © 2011-2022 走看看