There is a table "tbl" with unique values "val" from a certain range (eg, from 100 to 999).
Need to choose the guaranteed value, which does not yet exist in the table without the aid of an additional table with all existing values over the left association.
For example.
Helper table fill (whole range) with values from 1 to 9.
Basic table structure:
CREATE TABLE `ranger` (
`val` int(2) unsigned NOT NULL,
UNIQUE KEY `val` (`val`)
) ENGINE=MyISAM;
INSERT INTO `ranger` (`val`) VALUES (1), (2), (4), (5), (6), (7), (8);
To select a non-existent value from ranger
:
SELECT
val
FROM
ranger_helper
WHERE
val NOT IN(SELECT val FROM ranger)
ORDER BY
RAND()
LIMIT
1