mysql tutorial sql distinct delete duplicate records
How to use the distinct keyword in mysql to filter the list of duplicate values in a select statement
If you want to delete duplicate data when querying data, you can use the distinct keyword to filter duplicate values.
select distinct column_name from table_name;
Let's take a look at a simple example where we have a car list. Each car has an id, brand, type and color, but for now we simply list all 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 are in db. To get this extended query, look like this
select distinct brand from car;
the result is:
audi bmw
lexus
If you want to know how many different brands, then we must use the count group by to achieve the following example
select brand, count (brand) from car group by brand;
and the result is: brand count (brand)
audi 3
bmw 2
lexus 2
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.