MySQL SQL DISTINCT delete duplicate records

Source: Internet
Author: User
Tags mysql tutorial

MySQL Tutorial SQL distinct delete duplicate records

How to use the DISTINCT keyword in MySQL to filter duplicate values in the result list of a SELECT statement

If you want to delete duplicate data while querying the data, you can use the DISTINCT keyword to filter the duplicate values, and then look at the distinct usage below.

SELECT DISTINCT column_name from table_name;

Let's take a look at a simple example where we have a list of cars. Each car has an ID, brand, type and color, but now we are simply listing all the brands:

Select brand from Car;
The result is:audi                                                                                                                                           audi                                                                                                                                           audi                                                                                                                                           bmw                                                                                                                                           bmw                                                                                                                                           Lexus                                                                                                                                           lexus    

As you can see the brand shows several times. But what we want is to know how many different brands there are in DB. To get this extended query, as shown below

Select distinct brand from car;
The result is:
Audi BMW
Lexus



If you want to know how many times there are different brands, then we have to use Count group by to implement, see the example below


Select Brand, COUNT (brand) from car group by brand;
And the result Is:brand count (brand)
Audi 3
BMW 2
Lexus 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.