Ad Code

MySQL get missing IDs from table

 I have this table in MySQL, for example:

ID | Name
1  | Bob
4  | Adam
6  | Someguy

If you notice, there is no ID number (2, 3 and 5).

How can I write a query so that MySQL would answer the missing IDs only, in this case: "2,3,5" ?

 

SELECT 
    a.id+1 AS start, MIN(b.id) - 1 AS end
FROM 
    testtable AS a, testtable AS b
WHERE
    a.id < b.id 
GROUP BY a.id
HAVING start < MIN(b.id)

 

Post a Comment

0 Comments