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