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 when querying data, you can use the distinct keyword to filter duplicate values. The following describes how to use distinct.
Select distinct column_name from table_name;
Let's take a look at a simple example. We have a car list. Each car has an id, brand, type, and color, but now we just list all the brands:
Select brand from car;
The result is: audi bmw lexus
As you can see, the brand is displayed several times. But what we need is to know how many different brands there are in db. To obtain this extended query, as shown below:
Select distinct brand from car;
The result is:
Audi bmw
Lexus
If you want to know how many different brands there are, we must use count group by. Let's look at 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