Detailed Introduction to the MySQL Keyword Distinct

Detailed Introduction to the MySQL Keyword Distinct

Introduction to the usage of MySQL keyword Distinct

DDL

Prepare SQL:

create table test(id bigint not null primary key auto_increment,
   name varchar(10) not null,
   phone varchar(10) not null,
   email varchar(30) not null)engine=innodb;

Prepare Data:

 insert into test(name, phone, email)values('alibaba','0517','[email protected]');
  insert into test(name, phone, email)values('alibaba','0517','[email protected]');
  insert into test(name, phone, email)values('baidu','010','[email protected]');
  insert into test(name, phone, email)values('tencent','0755','[email protected]');
  insert into test(name, phone, email)values('vipshop','020','[email protected]');
  insert into test(name, phone, email)values('ctrip','021','[email protected]');
  insert into test(name, phone, email)values('suning','025','[email protected]');

The query data is shown in the following figure:

What is the meaning of the word "money"? title=

In the first case, use the Distinct keyword to query single-column data, as shown in the following figure:

What is the meaning of the word "a"? title=

Result: The name field is deduplicated, which meets expectations. The duplicate field value alibaba is indeed filtered out.

In the second case, use the Distinct keyword (in front) to query multiple columns of data, as shown in the following figure:

What is the meaning of the word "invisible"? title=

Result: The name field is deduplicated, but the result does not meet expectations. The duplicate field value alibaba is not filtered out.

In the second case, use the Distinct keyword (after) to query multiple columns of data, as shown in the following figure:

What is the meaning of the word "invisible"? title=

Result: The name field is deduplicated, but the result does not meet expectations. A SQL exception is thrown with error code 1064.

Solution:

Don't use subqueries, use grouping to solve it:

瑙e喅鏂瑰紡

Summary: If the SQL query can be solved with one statement, try not to increase the complexity of SQL, especially subqueries! ! !

The above is an explanation of the usage of the MySQL keyword Distinct. If you have any questions, please leave a message or discuss on this site. Thank you for reading and I hope it can help everyone. Thank you for your support of this site!

You may also be interested in:
  • How to optimize MySQL index function based on Explain keyword
  • Detailed explanation of mysql execution plan id is empty (UNION keyword)
  • jq.ajax+php+mysql to implement keyword fuzzy query (example explanation)
  • MySql multi-condition query statement with OR keyword
  • Explanation of the execution priority of mySQL keywords

<<:  Summary of practical methods for JS beginners to process arrays

>>:  Sample code for implementing two-way authentication with Nginx+SSL

Recommend

jQuery achieves fade-in and fade-out effects

Before using jQuery to complete the fade-in and f...

Implementation of HTML command line interface

HTML Part Copy code The code is as follows: <!D...

Detailed explanation of HTML basics (Part 1)

1. Understand the WEB Web pages are mainly compos...

How to update Ubuntu 20.04 LTS on Windows 10

April 23, 2020, Today, Ubuntu 20.04 on Windows al...

Understanding the MySQL query optimization process

Table of contents Parsers and preprocessors Query...

Introduction to encryption of grub boot program in Linux

Table of contents 1. What is grub encryption 2. g...

Tutorial diagram of installing centos7.3 on vmware virtual machine

VMware Preparation CentOS preparation, here is Ce...

Docker automated build Automated Build implementation process diagram

Automated build means using Docker Hub to connect...

Detailed explanation of CSS elastic box flex-grow, flex-shrink, flex-basis

The functions of the three attributes flex-grow, ...

Detailed explanation of TS object spread operator and rest operator

Table of contents Overview Object rest attribute ...

5 ways to migrate from MySQL to ClickHouse

Data migration needs to be imported from MySQL to...

The difference between VOLUME and docker -v in Dockerfile

There are obvious differences between volume moun...

Commonly used HTML format tags_Powernode Java Academy

1. Title HTML defines six <h> tags: <h1&...

Why do we need Map when we already have Object in JavaScript?

Table of contents 1. Don’t treat objects as Maps ...