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

  • Home
  • SEARCH
  • 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 8591527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:32:53+00:00 2026-06-11T23:32:53+00:00

I have a PHP program w/MySQL database which contains many records. Two columns of

  • 0

I have a PHP program w/MySQL database which contains many records. Two columns of particular relevance are incidentnumber and date. These both move forward only. However, sometimes a user enters data which is out of sequence; eg:

Incident Date
1        Jan 1 2000
2        Jan 1 2010
3        Jan 1 2002

It appears that incident 2 was entered with the wrong date, it should be Jan 1 2001.

Is there any way to query for records where the date is out of sequence? Or do I have to iterate through all records tracking last date to find the error?

ADDED NOTE: The incidents are not sequential (they might go 1,3,6,123, etc). Nor are the dates sequential. And these are columns in the same table.

  • 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-06-11T23:32:55+00:00Added an answer on June 11, 2026 at 11:32 pm

    This command selects any records for which there exists in the same table a record with a lower Incident number but a higher Date.

     SELECT * FROM TableName T1 WHERE EXISTS 
       (SELECT * FROM TableName T2 
          WHERE T2.Incident < T1.Incident AND T2.Date > T1.Date)
    

    This slightly more complex command will find only records for which are out of order in “both directions”, meaning they have an later dated record earlier in the file and an earlier dated record later in the file. This avoids the situation in which making a mistake in a very early record in the file will make all the subsequent records appear out of order. However, it will not catch a problem in the two records with the lowest or highest incident numbers.

     SELECT * FROM TableName T1 WHERE EXISTS 
       (SELECT * FROM TableName T2 
          WHERE T2.Incident < T1.Incident AND T2.Date > T1.Date)
     AND EXISTS
       (SELECT * FROM TableName T2 
          WHERE T2.Incident > T1.Incident AND T2.Date < T1.Date)
    

    Finally, as ruakh points out in the comments, the above query gives you ALL the out-of-order records. Although that is, technically, what you wanted it makes it difficult to find the “point of breakage” in the chain of dates. The following query will give you only the records where the chain gets messed up, does not require IncidentID to increase monotonically, and allows deletions of incidents.

     SELECT * FROM TableName T1 WHERE 
        Date < (SELECT Date FROM TableName T2 WHERE T2.IncidentID = 
       (SELECT MAX(IncidentID) FROM TableName T3 WHERE T3.IncidentID < T1.IncidentID))
     OR Date > (SELECT Date FROM TableName T2 WHERE T2.IncidentID = 
       (SELECT MAX(IncidentID) FROM TableName T3 WHERE T3.IncidentID > T1.IncidentID))
    

    (Not tested, since I don’t have a copy of MySQL handy).

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

Sidebar

Related Questions

I have an MySQL database which contains some data,it interacts with JSP pages,the thing
I have a membership program written with PHP and using mysql where users input
I have a CSV export script (enrdata_arch.php) which calls information from an existing database
I have a php script who access a MySQL database, all running in my
I have a MySQL database, php code to retrieve from the database, and an
I have a MySQL Database for a basic quiz program I'm making. The questions
I have written php program and uploaded on server. I want run this program
I have a PHP program that has been written keeping in mind a single
I have a SQL update statement I am running from inside a PHP program.
I have php mysql application and i want the phone numbers be saved in

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.