Some notes on mysql self-join deduplication

Some notes on mysql self-join deduplication

Let me briefly explain the functional scenario:

The data row fields are as follows:

name
started_at
type

In this table, name has duplicate values

Now we need to filter out a list when the type is determined, so that the name is not repeated and the record with the smallest started_at under the same name is found.

For example:

Event 1 2019-06-01 type1
Event 1 2019-06-02 type1
Event 1 2019-06-03 type1

Event 2 2019-06-03 type1
Event 2 2019-06-05 type1
Event 2 2019-06-07 type1

The sieve list should be:

Event 1 2019-06-01 type1
Event 2 2019-06-03 type1

It also needs to satisfy that started_at is greater than the current time

How should I write such sql?

The solution is:

Is to use left join itself

For example, s1 left join s2 on s1.name=s2.name and s2.started_at<s1.started_at and s2.started_at > now()

Finally where s2.id is null

SELECT
 s1.NAME,
 s1.started_at,
 
FROM
 tbl s1
 LEFT JOIN tbl s2 ON s1.`name` = s2.`name` 
  AND s1.started_at > s2.started_at 
  AND s2.started_at > now() 
WHERE
  s2.id IS NULL 
  AND s1.started_at > now() 
 AND s1.type = 'online_lecture'
ORDER BY
 s1.NAME,
 s1.started_at;

Does anyone have any better solution?

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:
  • Analysis of MySQL: single table distinct, multi-table group by query to remove duplicate records
  • mysql SELECT statement to remove duplicate information in a field
  • Detailed example of removing duplicate data in MySQL
  • One sql statement completes MySQL deduplication and keeps one
  • MySQL deduplication methods
  • MySQL development skills: JOIN update and data duplication check/deduplication
  • Mysql delete duplicate data Mysql data deduplication
  • Detailed explanation of two methods of deduplication in MySQL and example code
  • A simple method to merge and remove duplicate MySQL tables
  • How to optimize MySQL deduplication operation to the extreme
  • MySQL optimization tips: analysis of duplicate removal implementation methods [millions of data]

<<:  Linux traceroute command usage detailed explanation

>>:  Detailed explanation of the pitfalls of mixing npm and cnpm

Recommend

Use of hasOwnProperty method of js attribute object

Object's hasOwnProperty() method returns a Bo...

18 common commands in MySQL command line

In daily website maintenance and management, a lo...

mysql subquery and join table details

Table of contents 1. What is a subquery? 2. Self-...

How to use border-image to implement text bubble border sample code

During the development activity, I encountered a ...

HTML table tag tutorial (12): border style attribute FRAME

Use the FRAME property to control the style type ...

Solve the problem of docker log mounting

The key is that the local server does not have wr...

Friendly Alternatives to Find Tool in Linux

The find command is used to search for files in a...

Eclipse configures Tomcat and Tomcat has invalid port solution

Table of contents 1. Eclipse configures Tomcat 2....

Several ways to add timestamps in MySQL tables

Scenario: The data in a table needs to be synchro...

Detailed example of HTML element blocking Flash

Copy code The code is as follows: wmode parameter...

Implementation of MySQL joint index (composite index)

Joint Index The definition of the joint index in ...

JS implements jQuery's append function

Table of contents Show Me The Code Test the effec...

Use HTML to write a simple email template

Today, I want to write about a "low-tech&quo...

A brief discussion on docker-compose network settings

Networks usage tutorial Official website docker-c...