Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6989015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:09:26+00:00 2026-05-27T19:09:26+00:00

I’ve seen examples for duplicate row manipulation, but I can’t figure out how to

  • 0

I’ve seen examples for duplicate row manipulation, but I can’t figure out how to map them to solve my problem.

+----+------------+------+---------+
| id | date       | file | status  |
+----+------------+------+---------+
|  1 | 2011-12-01 |    1 | Pending |
|  2 | 2011-12-02 |    1 | Pending |
|  3 | 2011-12-03 |    1 | Done    |
|  4 | 2011-12-04 |    1 | Pending |
|  5 | 2011-12-05 |    1 | Done    |
|  6 | 2011-12-06 |    1 | Pending |
|  7 | 2011-12-07 |    1 | Pending |
|  8 | 2011-12-08 |    1 | Pending |
|  9 | 2011-12-09 |    2 | Pending |
| 10 | 2011-12-10 |    2 | Pending |
| 11 | 2011-12-11 |    3 | Pending |
| 12 | 2011-12-12 |    4 | Done    |
| 13 | 2011-12-13 |    5 | Pending |
| 14 | 2011-12-14 |    5 | Done    |
| 15 | 2011-12-15 |    5 | Pending |
+----+------------+------+---------+

For each file in the table:

  1. I need to first select/delete any row where status=’Pending’, and its date is older than the youngest date for any row where status=’Done’. For the example, this would select/delete rows with id 1, 2, 4, and 13.

  2. I need to next select/delete any row where status=’Pending’ and it’s not the oldest date where status=’Pending’. For the example, this would select/delete rows with id 7, 8, and 10.

The resulting table is:

+----+------------+------+---------+
| id | date       | file | status  |
+----+------------+------+---------+
|  3 | 2011-12-03 |    1 | Done    |
|  5 | 2011-12-05 |    1 | Done    |
|  6 | 2011-12-06 |    1 | Pending |
|  9 | 2011-12-09 |    2 | Pending |
| 11 | 2011-12-11 |    3 | Pending |
| 12 | 2011-12-12 |    4 | Done    |
| 14 | 2011-12-14 |    5 | Done    |
| 15 | 2011-12-15 |    5 | Pending |
+----+------------+------+---------+

This will create and populate the test table in MySQL:

CREATE TABLE test (
id int(11) NOT NULL AUTO_INCREMENT,
date date DEFAULT NULL,
file int(11) DEFAULT NULL,
status varchar(45) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;

INSERT INTO test VALUES (1,’2011-12-01′,1,’Pending ‘),(2,’2011-12-02’,1,’Pending ‘),(3,’2011-12-03′,1,’Done’),(4,’2011-12-04′,1,’Pending ‘),(5,’2011-12-05′,1,’Done’),(6,’2011-12-06′,1,’Pending ‘),(7,’2011-12-07’,1,’Pending ‘),(8,’2011-12-08’,1,’Pending ‘),(9,’2011-12-09’,2,’Pending ‘),(10,’2011-12-10’,2,’Pending ‘),(11,’2011-12-11’,3,’Pending ‘),(12,’2011-12-12′,4,’Done’),(13,’2011-12-13′,5,’Pending ‘),(14,’2011-12-14′,5,’Done’),(15,’2011-12-15′,5,’Pending ‘);


Thanks to ziesemer for the correct SELECT queries–I learned a lot from them. Unfortunately, it appears that MySQL doesn’t allow DELETE with a subquery, so I converted ziesemer’s answer to use JOINS instead. But I’m a SQL noob, so please correct if these could be improved:

SELECT DISTINCT t1.* FROM test t1 INNER JOIN test t2
 WHERE t1.file = t2.file
   AND t1.status = 'Pending'
   AND t2.status = 'Done'
   AND t1.date < t2.date;

SELECT DISTINCT t1.* FROM test t1 INNER JOIN test t2
 WHERE t1.file = t2.file
   AND t1.status = 'Pending'
   AND t2.status = 'Pending'
   AND t1.date > t2.date;

To delete, replace the SELECT line with:

DELETE t1 FROM test t1 INNER JOIN test t2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:09:27+00:00Added an answer on May 27, 2026 at 7:09 pm

    I have these tested, working independently – though the 2nd must be executed after the 1st to get the results you provided in your example. I am having some difficulty getting them to work as one Select, as the 2nd query is dependent upon the state of the table after the 1st is complete…

    Select *
        From my_table t1
        Where (status = 'Pending'
            And date < (
                Select Max(date)
                    From my_table t2
                    Where t2.file = t1.file
                        And t2.status = 'Done'));
    
    Select *
        From my_table t1
        Where (status = 'Pending'
            And date > (
                Select Min(date)
                    From my_table t2
                    Where t2.file = t1.file
                        And t2.status = 'Pending'));
    

    (I’ll give a +1 to anyone else who’s answer can do this in one query, while producing the same, accurate results – I’m stumped, for now.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.