Detailed example of using the distinct method in MySQL

Detailed example of using the distinct method in MySQL

A distinct

Meaning: distinct is used to query the number of unique records, that is, distinct returns the number of unique fields (count(distinct id)). The reason is that distinct can only return its target field, but not other fields.

Usage Notes:

1.distinct [query field] must be placed at the beginning of the field to be queried, that is, in the first parameter;
2. It can only be used in SELECT statements, not in INSERT, DELETE, or UPDATE;
3.DISTINCT means to concatenate all the following parameters to get non-duplicate records, that is, each row of concatenated parameters is unique.
4. It cannot be used together with all. By default, all results are returned during the query.

1.1 Check only one field

Checking for duplicates in a field means selecting a column of non-duplicate data in that field.

Example table: psur_list

To remove duplicates from the PLAN_NUMBER field, use the following statement:

SELECT DISTINCT PLAN_NUMBER FROM psur_list;

The results are as follows:

1.2 Multiple fields deduplication

De-duplicate multiple fields, which means selecting a record that is a concatenation of multiple fields, and all non-duplicate records

Example table: psur_list

To remove duplicates from the PLAN_NUMBER and PRODUCT_NAME fields, use the following statement:

SELECT DISTINCT PLAN_NUMBER,PRODUCT_NAME FROM psur_list;

The results are as follows:

Expected result: Only the first parameter PLAN_NUMBER has a unique value

Solution 1: Use the group_concat function

Statement:

SELECT GROUP_CONCAT(DISTINCT PLAN_NUMBER) AS PLAN_NUMBER,PRODUCT_NAME FROM psur_list GROUP BY PLAN_NUMBER

Solution 2: Use group by

Statement:

SELECT PLAN_NUMBER,PRODUCT_NAME FROM psur_list GROUP BY PLAN_NUMBER

The results are as follows:

1.3 Dealing with null

distinct will not filter out null values, and the returned results will include null values

The table psur_list is as follows:

To remove duplicates from the COUNTRY field, use the following statement:

SELECT DISTINCT COUNTRY FROM psur_list

The results are as follows:

1.4 Synonymous with distinctrow

Statement:

SELECT DISTINCTROW COUNTRY FROM psur_list

The results are as follows:

Using distinct in binary aggregation functions

In aggregate functions, DISTINCT is usually used in conjunction with COUNT. count() will filter out null items

Statement:

SELECT COUNT(DISTINCT COUNTRY) FROM psur_list

The results are as follows: [There are actually 4 records containing null items. After executing the statement, the null items are filtered out and the calculation is 3]

This is the end of this article about the detailed example of using the distinct method in MySQL. For more relevant MySQL deduplication distinct content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of three deduplication methods in SQL
  • How to optimize MySQL deduplication operation to the extreme
  • A simple method to merge and remove duplicate MySQL tables
  • MySQL deduplication methods
  • Detailed explanation of two methods of deduplication in MySQL and example code
  • SQL Learning Notes 5: How to remove duplicates and assign values ​​to newly added fields
  • Summary of SQL deduplication methods

<<:  Detailed explanation of several solutions for JavaScript interruption requests

>>:  Detailed tutorial for installing influxdb in docker (performance test)

Recommend

Detailed steps to download Tomcat and put it on Linux

If you have just come into contact with Linux, th...

HTML table markup tutorial (16): title horizontal alignment attribute ALIGN

By default, the table title is horizontally cente...

Windows 2019 Activation Tutorial (Office2019)

A few days ago, I found that the official version...

How to deploy Solidity smart contracts using ethers.js

If you have developed DApps on Ethereum, you may ...

How to use Element in React project

This is my first time using the element framework...

Vue state management: using Pinia instead of Vuex

Table of contents 1. What is Pinia? 2. Pinia is e...

Oracle deployment tutorial in Linux environment

1. Environment and related software Virtual Machi...

Detailed explanation of CSS image splicing technology (sprite image)

CSS image splicing technology 1. Image stitching ...

Optimize MySQL with 3 simple tweaks

I don't expect to be an expert DBA, but when ...

Centos7 mysql database installation and configuration tutorial

1. System environment The system version after yu...

The url value of the src or css background image is the base64 encoded code

You may have noticed that the src or CSS backgroun...

Mysql string interception and obtaining data in the specified string

Preface: I encountered a requirement to extract s...

Web Design Tutorial (5): Web Visual Design

<br />Previous article: Web Design Tutorial ...