mysql distinct_使用MySQL DISTINCT来去除重复行记录

本文介绍如何使用 MySQL 的 DISTINCT 操作符来消除查询结果中的重复行。通过实例演示如何选取唯一值,处理 NULL 值,并结合 COUNT 函数进行计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Summary: in this tutorial, you will learn how to use MySQL DISTINCToperatorwith the SELECTstatement to eliminate duplicate rows in the result set.

When querying data from a table, you may get duplicate rows. In order to remove the duplicate rows, you use the DISTINCToperator in the SELECT statement. The syntax of using the DISTINCToperator is as follows:SELECT DISTINCT columns FROM table_name WHERE where_conditions

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

First, we query the last names of employees from the employeestable using the SELECTstatement as follows:SELECT lastname FROM employees ORDER BY lastname

AAffA0nNPuCLAAAAAElFTkSuQmCC

Some employees has the same last name  Bondur, Firrelli, etc. To remove the duplicate last names, you use the DISTINCToperator in the SELECTclause as follows:SELECT DISTINCT lastname FROM employees ORDER BY lastname

AAffA0nNPuCLAAAAAElFTkSuQmCC

The duplicate last names are eliminated in the result set when we use the DISTINCT operator.

MySQL DISTINCT and NULL values

If a column has NULLvalues and you use the DISTINCToperator for that column, MySQL will keep one NULLvalue and eliminate the other because the DISTINCToperator treats all NULLvalues as the same value.

For example, in the customerstable, we have many rows with state column has NULLvalues. When we use the DISTINCToperator to query states of customers, we will see distinct states plus a NULL value as the following query:SELECT DISTINCT state FROM customers

AAffA0nNPuCLAAAAAElFTkSuQmCC

MySQL DISTINCT with multiple columns

You can use the DISTINCToperator with more than one column. The combination of all columns will be used to define the uniqueness of the row in the result set.

For example, to get the unique combination of city and state from the customerstable, you use the following query:SELECT DISTINCT state, city FROM customers WHERE state IS NOT NULL ORDER BY state, city

AAffA0nNPuCLAAAAAElFTkSuQmCC

Without the DISTINCToperator, you will get duplicate combination state and city as follows:SELECT state, city FROM customers WHERE state IS NOT NULL ORDER BY state, city

AAffA0nNPuCLAAAAAElFTkSuQmCC

DISTINCT vs. GROUP BY Clause

If you use the GROUP BY clause in the SELECTstatement without using aggregate functions, the GROUP BY clause will behave like the DISTINCT operator. The following queries produce the same result set:SELECT DISTINCT state FROM customers; SELECT state FROM customers GROUP BY state;

The difference between DISTINCToperator and GROUP BY clause is that the GROUP BY clause sorts the result set whereas the DISTINCToperator does not.

MySQL DISTINCT and COUNT aggregate function

The DISTINCT operator is used with the COUNT function to count unique records in a table. In this case, it ignores the NULLvalues. For example, to count the unique states of customers in the U.S., you use the following query:SELECT COUNT(DISTINCT state) FROM customers WHERE country = 'USA';

AAffA0nNPuCLAAAAAElFTkSuQmCC

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

Related Tutorials

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值