Mysql some complex sql statements (query and delete duplicate rows)

Mysql some complex sql statements (query and delete duplicate rows)

1. Find duplicate rows

SELECT * FROM blog_user_relation a WHERE (a.account_instance_id,a.follow_account_instance_id) 
IN (SELECT account_instance_id,follow_account_instance_id FROM blog_user_relation GROUP BY account_instance_id, follow_account_instance_id HAVING
 COUNT(*) > 1)

2. Delete duplicate rows (keep one)

PS: Because of MySQL's delete, if the where condition of the deleted table contains in, and this table also exists in in, then it cannot be deleted.

/*Create a temporary table*/
CREATE TABLE blog_user_relation_temp AS
(
 SELECT * FROM blog_user_relation a WHERE 
 (a.account_instance_id,a.follow_account_instance_id) 
 IN ( SELECT account_instance_id,follow_account_instance_id FROM blog_user_relation GROUP BY account_instance_id, follow_account_instance_id HAVING COUNT(*) > 1)
 AND 
 relation_id 
 NOT IN (SELECT MIN(relation_id) FROM blog_user_relation GROUP BY account_instance_id, follow_account_instance_id HAVING COUNT(*)>1));

/*delete data*/
DELETE FROM `blog_user_relation` WHERE relation_id IN (SELECT relation_id FROM blog_user_relation_temp);

/*Delete temporary table*/
DROP TABLE blog_user_relation_temp;

You may also be interested in:
  • Use SQL batch replace statements to modify, add, and delete field contents
  • SQL statement to delete duplicate records and keep only one
  • Must-know SQL statements (IV) Data deletion and update
  • Must-know SQL statements (Part 2) Create table, modify table structure, delete table
  • Example code for creating and deleting constraints using SQL statements
  • Oracle uses SQL statements to add fields (SQL delete field statements)
  • SQL statement to batch delete the specified prefix table in MySQL
  • How to delete and add foreign keys and primary keys using SQL statements
  • Comparison of MySQL Delete and Truncate statements
  • Simply understand the differences in the principles of common SQL delete statements

<<:  Implementing a puzzle game with js

>>:  Listen directive example analysis in nginx

Recommend

Implementation code for installing vsftpd in Ubuntu 18.04

Install vsftpd $ sudo apt-get install vsftpd -y S...

Parsing Linux source code epoll

Table of contents 1. Introduction 2. Simple epoll...

How to embed other web pages in a web page using iframe

How to use iframe: Copy code The code is as follo...

Detailed explanation of docker visualization graphics tool portainer

Table of contents 1. Introduction to Portainer 2....

Detailed explanation of various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

How to use HTML+CSS to create TG-vision homepage

This time we use HTML+CSS layout to make a prelim...

Detailed tutorial on installing VirtualBox and Ubuntu 16.04 under Windows system

1. Software Introduction VirtualBox VirtualBox is...

A brief discussion on the font settings in web pages

Setting the font for the entire site has always b...

How to use negative margin technology to achieve average layout in CSS

We usually use float layout to solve the compatib...

Tutorial on installing MySQL database and using Navicat for MySQL

MySQL is a relational database management system ...