BlogPhotosContact

Topics

Finding gaps in sequence

Here is a subselect query I have used successfully to get a list of missing serial numbers in a auto-incremented (or not so automatic in my case) sequence :

SELECT A.number + 1
FROM orders AS A
WHERE NOT EXISTS (
SELECT B.number FROM orders AS B
WHERE A.number + 1 = B.number)
GROUP BY A.number;

Yes, simple but effective :)

Will work only in MySQL 4.1+ though because it's a subselect.

Comments (0)  Permalink