简体   繁体   中英

How to find wrong order in MySQL?

I have a table with 2 columns:

id (int not null, auto_incremented), and insert_date (date) .

The table is filled sometimes from outside source where I don't have control over the data.

Time to time it happens that the id values are not in right order given by the date. How can I find these anomalities?

Example of wrong data:

1, 2014-02-03
2, 2014-02-01  <<<< WRONG! It should be between 2014-02-03 and 2014-02-04
3, 2014-02-04
4, 2014-02-04
5, 2014-03-01

You can use a query like this:

SELECT DISTINCT
  t1.*
FROM
  tablename t1 INNER JOIN tablename t2
  ON t1.id>t2.id
WHERE
  t1.datefield<t2.datefield

however, it is really not a problem to have dates in the "wrong" order since you can always order by the date column.

Please see fiddle here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM