Detailed description of MySQL Data deduplication instances

Source: Internet
Author: User

Detailed description of MySQL Data deduplication instances

Detailed description of MySQL deduplication instances

There are two Repeated Records. One is a completely repeated record, that is, all fields are duplicated, and the other is a record with some fields repeated. For the first type of repetition, it is easy to solve. You only need to use the distinct keyword in the query statement to remove duplicates. Almost all Database Systems Support the distinct operation. This type of repetition occurs mainly because the table is designed for weeks and can be avoided by adding a primary key or a unique index column to the table.

select distinct * from t;

For the second type of repetition problem, it is usually required to query any record in the repetition record. Assume that table t has three fields: id, name, and address. The id is the primary key and the duplicate fields are name and address. The unique result set of these two fields must be obtained.

-- Oracle and MySQL: use related subqueries

select * from t t1 where t1.id = (select min(t2.id)  from t t2  where t1.name = t2.name and t1.address = t2.address);

-- Hive only supports subqueries in the from clause. subqueries must have names and must be unique in columns.

select t1.* from t t1,   (select name, address, min(id) id from t group by name, address) t2 where t1.id = t2.id; 

-- You can also use the row_number () analysis function of hive.

select t.id, t.name, t.address from (select id, name, address,row_number() over (distribute by name, address sort by id) as rn      from t) t  where t.rn=1;

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.