The difference between distinct and group by in MySQL

The difference between distinct and group by in MySQL

Simply put, distinct is used to remove duplicates, while group by is designed to aggregate statistics. The two have some similarities in the functions they can achieve, but they should be carefully distinguished.

The simple deduplication operation using DISTINCT is faster than GROUP BY.

distinct supports single-column and multi-column deduplication.
The single-column deduplication method is simple and easy to understand, that is, only one identical value is retained.
Multiple columns are deduplicated based on the specified deduplicated column information, that is, only when all the specified column information is the same will it be considered as duplicate information.

The frequency of group by is relatively high, but just like its function, its purpose is to perform aggregate statistics. Although it is also possible to achieve the function of deduplication, this is not its strong point.

the difference:

1) distinct simply removes duplicate rows from the result;

Group by means grouping by specified columns, and generally aggregate functions are used in select at this time.

2) distinct is to display different records.

Group by means to sort the records by category before querying.

GROUP BY must include an aggregate function in the query results, while DISTINCT does not.

This is the end of this article about the difference between distinct and group by in MySQL. For more information about the difference between distinct and group by, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on MySQL select optimization solution
  • MySQL select results to perform update example tutorial
  • Solve the problem that MySQL read-write separation causes data not to be selected after insert
  • How MySQL Select Statement is Executed
  • Detailed example of using the distinct method in MySQL
  • Should I use distinct or group by to remove duplicates in MySQL?
  • Let's talk about the LIMIT statement in MySQL in detail
  • MySQL series tutorial on understanding the use of union (all) and limit and exists keywords
  • The impact of limit on query performance in MySQL
  • Use of select, distinct, and limit in MySQL

<<:  Detailed explanation of the this pointing problem in JavaScript

>>:  A brief discussion on simulating multi-threaded and multi-process crashes in Linux

Recommend

NULL and Empty String in Mysql

I recently came into contact with MySQL. Yesterda...

HTML+CSS to achieve surround reflection loading effect

This article mainly introduces the implementation...

Detailed explanation of the getBoundingClientRect() method in js

1. getBoundingClientRect() Analysis The getBoundi...

Detailed explanation of the wonderful CSS attribute MASK

This article will introduce a very interesting at...

Getting Started Tutorial on Using TS (TypeScript) in Vue Project

Table of contents 1. Introducing Typescript 2. Co...

Detailed steps to expand LVM disk in Linux

1. Add a hard disk 2. Check the partition status:...

How to Clear Disk Space on CentOS 6 or CentOS 7

Following are the quick commands to clear disk sp...

How to achieve seamless token refresh

Table of contents 1. Demand Method 1 Method 2 Met...

MySQL detailed single table add, delete, modify and query CRUD statements

MySQL add, delete, modify and query statements 1....

A brief discussion on HTML table tags

Mainly discuss its structure and some important pr...

How to install redis5.0.3 in docker

1. Pull the official 5.0.3 image [root@localhost ...