A simple method to merge and remove duplicate MySQL tables

A simple method to merge and remove duplicate MySQL tables

Scenario:

The crawled data generates a data table with the same structure as another main table, which needs to be merged and deduplicated

Solution: (direct example)

First create two tables pep and pep2, where pep is the main table

CREATE TABLE IF NOT EXISTS `pep/pep2`(
`id` INT UNSIGNED AUTO_INCREMENT,
`no` VARCHAR(100) NOT NULL,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

Then insert two pieces of data into pep, and insert a piece of data identical to that in pep into pep2

insert into pep(no) values('abc');
insert into pep(no) values('caa');

insert into pep2(no) values('abc');

Insert pep2 data into pep

insert into pep (no) select no from pep2;

Group to recreate a new temporary table tmp

create table tmp select id,no from pep group by no;

Note: After creating this table, the ID field type is no longer a primary key auto-increment

 You may also get an error ```Syntax error or access violation: 1055 Expression #1 of SELECT 
 list is not in GROUP BY clause and contains nonaggregated 
 column 'XXX.Y.ZZZZ' which is not functionally dependent on 
 columns in GROUP BY clause; this is incompatible with
 sql_mode=only_full_group_by
 ```
 Solution: Execute the following two commands:
 ```
 mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 
 mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
 ```

Delete the pep table and rename the tmp table to pep

drop table pep;
alter table tmp rename to pep;

Check the desc structure and select * from pep and find that the field type of id has changed. You need to change it back to the original type.

alter table pep add primary key (id);
alter table pep modify id int auto_increment;

You can also use join to remove duplicates. To be faster, you can add a field (which can be the md5 value of several fields combined), create a unique index unique for this field, and automatically filter out duplicate data when inserting data in the future.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Summary of three deduplication methods in SQL
  • Detailed example of using the distinct method in MySQL
  • How to optimize MySQL deduplication operation to the extreme
  • MySQL deduplication methods
  • Detailed explanation of two methods of deduplication in MySQL and example code
  • SQL Learning Notes 5: How to remove duplicates and assign values ​​to newly added fields
  • Summary of SQL deduplication methods

<<:  Solve the problem of inconsistent front and back end ports of Vue

>>:  Ubuntu 18.04 obtains root permissions and logs in as root user

Recommend

MySQL data table partitioning strategy and advantages and disadvantages analysis

Table of contents Why do we need partitions? Part...

Docker custom network container interconnection

Table of contents Preface –link Custom Network As...

Detailed explanation of the core concepts and basic usage of Vuex

Table of contents introduce start Install ① Direc...

Vue.js implements the code of clicking the icon to zoom in and leaving

The previous article introduced how Vue can reali...

js to achieve a simple lottery function

This article shares the specific code of js to im...

How to build Jenkins+Maven+Git continuous integration environment on CentOS7

This article takes the deployment of Spring boot ...

How to modify the group to which a user belongs in Linux

Modify the group to which a user belongs in Linux...

Detailed explanation of importing/exporting MySQL data in Docker container

Preface We all know that the import and export of...

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

Implementation of the login page of Vue actual combat record

Table of contents 1. Preliminary preparation 1.1 ...

Teach you how to insert 1 million records into MySQL in 6 seconds

1. Idea It only took 6 seconds to insert 1,000,00...

Detailed examples of using JavaScript event delegation (proxy)

Table of contents Introduction Example: Event del...