MySQL Filters part of field duplicate data

Source: Internet
Author: User

Select distinct can remove duplicate records.

Disctinct ignores duplicate records, but it ignores identical duplicate records, not records that are duplicated in one of these fields, or where a distinct queries a field so that multiple fields are not.

So using aggregate functions and GROUP by implementations

Note: Group by can only be used with aggregate functions

Example table

ID username Password Trdesc ........ ......... ...

1 A abcdef QR

2 A abcdef W34

3 A bbbbbb AD

4 B aaaaaa ASDF

Query username and password combination of conditions can not be repeated query results (this can be repeated, not to mention that this is a mess)

When username and password Repeat, take the record with the largest ID:

SELECT * FROM MyTable where ID in (select Max (ID) from MyTable Group by Username,password)

When username and password Repeat, take the record with the lowest ID:

SELECT * FROM MyTable where ID in (select min (ID) from MyTable a group by Username,password)

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.