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

JavaScript to implement the function of changing avatar

This article shares the specific code of JavaScri...

Tips for implementing multiple borders in CSS

1. Multiple borders[1] Background: box-shadow, ou...

Deploy Nginx+Flask+Mongo application using Docker

Nginx is used as the server, Mongo is used as the...

Solution to the problem that input in form cannot be submitted when disabled

I wrote a test program before, in which adding and...

HTML image img tag_Powernode Java Academy

summary Project description format <img src=&q...

Detailed configuration of mysql8.x docker remote access

Table of contents Environmental conditions Errors...

HTML table markup tutorial (43): VALIGN attribute of the table header

In the vertical direction, you can set the alignm...

JavaScript to display hidden form text

This article shares the specific code of JavaScri...

Full HTML of the upload form with image preview

The upload form with image preview function, the ...

Implementation of Docker deployment of Django+Mysql+Redis+Gunicorn+Nginx

I. Introduction Docker technology is very popular...

The difference between the four file extensions .html, .htm, .shtml and .shtm

Many friends who have just started to make web pag...

MySQL5.7.21 decompressed version installation detailed tutorial diagram

Since I often install the system, I have to reins...