Vitor, I’d do it this way.
Would take the amount: MAX()
from your table. Let’s say 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 precedent to feed this table with the range of data you want to search.
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 ;
Then we run the trial:
CALL dowhile();
SELECT * FROM incr;
Upshot:
Id
1
2
3
...
100
After that we consult using the NOT EXISTS
to take 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 non-existent codes in your table.
Vitor, I advise you to include more details. Such as: details of your table, when setting up a
ID
of these was used?? It is inserted in another table? How does it work? Then we can help you. If they are in separate tables is quite simple, just use aNOT EXISTS
orEXISTS
depends on where the query will start. ExampleSELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);
– Fernando A.W.
@ Fernando A.W. , These records are entered manually, which caused this mess in the code to skip some at the time of registration.
– Vitor Molina
but they stay on the same table? I guess I got it, due to being
insert
manual you want to take the values that were not used.– Fernando A.W.
Correct Fernando A.W., get the records within only one table!
– Vitor Molina
Vitor, from a look at the answer I put, should meet your need.
– Fernando A.W.