title: Mysql Check integer availability
body: <p>I would like to know if there is a possibility to create a query that returns the smallest integer available. It's a bit complicated to explain, but with an example it facilitates:</p>
  
  <p>Code: 1 2 4 5 6</p>
  
  <p>In this case, I would like the query to return the number 3, since it is the smallest integer that is still available.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have the followings tables:</p>
  
  <pre><code>Itens
  +----+-----------+------+
  | id | name      | code |
  +----+-----------+------+
  | 1  | produto 1 | 1    |
  +----+-----------+------+
  | 2  | produto 2 | 2    |
  +----+-----------+------+
  | 3  | produto 3 | 4    |
  +----+-----------+------+
  | 4  | produto 4 | 6    |
  +----+-----------+------+
  
  Categories
  +----+------------+------------+
  | id | name       | company_id |
  +----+------------+------------+
  | 1  | Category 1 |     1      |
  +----+------------+------------+
  | 2  | Category 2 |     2      |
  +----+------------+------------+
  
  Companies
  +----+-----------+
  | id | name      |
  +----+-----------+
  | 1  | Company 1 |
  +----+-----------+
  | 2  | Company 2 |
  +----+-----------+
  </code></pre>
  
  <p>I need do it for each company int the trigger. I try the code bellow, but doesn't work.</p>
  
  <pre><code>CREATE TRIGGER `updateCodigoNull`
  BEFORE INSERT ON `itens`
  FOR EACH ROW
    BEGIN
      IF (NEW.codigo IS NULL)
      THEN
        SET NEW.codigo = (SELECT b.codigo - 1
                          FROM (
                                 (SELECT
                                    2 AS 'codigo',
                                    1 AS 'missing',
                                    1 AS 'previous'
                                  FROM itens
                                    INNER JOIN item_categories ON itens.category_id = item_categories.id
                                  WHERE NOT EXISTS(SELECT 1
                                                   FROM itens
                                                   WHERE codigo = 1) AND item_categories.company_id = 2
                                  LIMIT 1)
                                 UNION
                                 (SELECT
                                    codigo,
                                    IF(@previous != 0 AND @previous &lt; codigo - 1, 1, 0) AS missing,
                                    @previous := codigo
                                  FROM itens
                                    INNER JOIN item_categories
                                      ON itens.category_id = item_categories.id
                                    , (SELECT @previous := 0) a
                                  WHERE item_categories.company_id = 2
                                 )) b
                          WHERE b.missing = 1
                          ORDER BY b.codigo
                          LIMIT 1);
      END IF;
    END
  </code></pre>