Home > MySQL > Finding Missing Values In A MySQL Table

Finding Missing Values In A MySQL Table

September 19th, 2008 Leave a comment Go to comments

If you have a table of incremental values it can be hard to find out which ones are missing. The only solution might be to write a script to get all the data from the database and see which ones are missing. However, there is a way of doing this without using a script.

Using a standard select query like this:

SELECT * FROM table;

Gets the following data:

1
3
10
23

We can see that values are missing, but which ones? The following query will show us where the gaps are in the data of the table.

SELECT t1.id+1 as Missing
FROM table as t1
LEFT JOIN table as t2 ON t1.id+1 = t2.id
WHERE t2.id IS NULL
ORDER BY t1.id;

Produces the following result.

2
4
11
24

However, this only tell us where the gaps are, not how long they are. To get the range of where the gaps from and to we need to do something a little more complex.

SELECT
t1.id+1 AS 'Missing From',
MIN(t2.id) - 1 AS 'To'
FROM table AS t1, table AS t2
WHERE t1.id < t2.id
GROUP BY t1.id
HAVING t1.id < MIN(t2.id) - 1;

This query gives the following result.

Missing From To
2 2
4 9
11 22

Using this dataset we can figure out where the gaps in the data are and perhaps do something with them.

Categories: MySQL Tags: , , , , , ,
  1. Jamie Bicknell
    September 19th, 2008 at 09:44 | #1

    Nice Work!

    My MySql keeps pulling up an error on the last SQL statement. There error is: #1054 – Unknown column ‘t1.id’ in ‘having clause’

    Versions are:
    phpMyAdmin – 2.11.5.2
    MySQL client version: 5.0.45

    Any ideas?

  2. September 19th, 2008 at 10:38 | #2

    Odd this… It works in MySQL query browser, so I thought it must be an issue with phpMyAdmin. And it was! For some reason it has a problem with the table.column reference in the having command.

    I found the solution was to add quotes around the name, like this:

    HAVING 't1.ID' < MIN(t2.ID) - 1

    Let us know if that works!

  3. Nalle
    February 1st, 2009 at 23:01 | #3

    Great example! Thanks!

  4. MySQLslave
    October 6th, 2009 at 08:20 | #4

    Just what I needed, thanks! Didn’t fancy going through a several thousand long dataset finding that one missing id…

  1. No trackbacks yet.