Vitor, I would do it as follows.
It would take the value: MAX()
of your table. Let's say it's 100. Then it would generate a temporary table with values from 1 to 100.
CREATE TABLE 'incr' (
'Id' int(11) NOT NULL auto_increment,
PRIMARY KEY ('Id')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Next a procedure to feed this table with the data range you want to fetch.
DELIMITER ;;
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 100;
WHILE v1 > 0 DO
INSERT incr VALUES (NULL);
SET v1 = v1 - 1;
END WHILE;
END;;
DELIMITER ;
Next, we execute the procedure:
CALL dowhile();
SELECT * FROM incr;
Result:
Id
1
2
3
...
100
After this we query using NOT EXISTS
to get the values that are not in your table:
SELECT DISTINCT ID FROM incr
WHERE NOT EXISTS (SELECT * FROM SUA_TABELA_AKI
WHERE SUA_TABELA_AKI.ID= incr.ID);
And this way you will have all the codes that do not exist in your table.