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

Summary of common optimization operations of MySQL database (experience sharing)

Preface For a data-centric application, the quali...

Analysis of the principle of Nginx using Lua module to implement WAF

Table of contents 1. Background of WAF 2. What is...

Solve the compatibility issue between MySQL 8.0 driver and Alibaba Druid version

This article mainly introduces the solution to th...

Detailed explanation of the use of Docker commit

Sometimes you need to install certain dependencie...

Let's talk about what JavaScript's URL object is

Table of contents Overview Hash Properties Host p...

Detailed explanation of Javascript basics loop

Table of contents cycle for for-in for-of while d...

The whole process of Vue page first load optimization

Table of contents Preface 1. Image Optimization 2...

Detailed explanation of Jquery datagrid query

Table of contents Add code to the Tree item; 1. S...

How to use glog log library in Linux environment

Generate Linux library The Linux version uses cen...

Vue implements small form validation function

This article example shares the specific code of ...

Vue+Element realizes paging effect

This article example shares the specific code of ...

Element dynamic routing breadcrumbs implementation example

To master: localStorage, component encapsulation ...

How to capture exceptions gracefully in React

Table of contents Preface ErrorBoundary Beyond Er...