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

Overview and Introduction to Linux Operating System

Table of contents 1. What is an Operating System ...

Design Theory: Textual Expression and Usability

<br />In text design, we usually focus on th...

MySQL database transaction example tutorial

Table of contents 1. What is a transaction? 2. Th...

Install tomcat and deploy the website under Linux (recommended)

Install jdk: Oracle official download https://www...

Vue implements image dragging and sorting

This article example shares the specific code of ...

Detailed explanation of the use of Arguments object in JavaScript

Table of contents Preface Basic Concepts of Argum...

js uses cookies to remember user page operations

Preface During the development process, we someti...

Detailed explanation of Nginx passively checking the server's survival status

introduce Monitors the health of HTTP servers in ...

Detailed explanation of Vue development Sort component code

Table of contents <template> <ul class=&...

JavaScript color viewer

This article example shares the specific code of ...

Detailed explanation of the use of Vue's new built-in components

Table of contents 1. Teleport 1.1 Introduction to...

vue+echarts realizes the flow effect of China map (detailed steps)

@vue+echarts realizes the flow effect of China ma...

MySQL uses init-connect to increase the implementation of access audit function

The mysql connection must first be initialized th...

Vue uses better-scroll to achieve horizontal scrolling method example

1. Implementation principle of scrolling The scro...