title: Problematic LAST_INSERT_ID
body: <pre><code>CREATE FUNCTION `getSequenceNumber` (
  company_id INTEGER, sequence_name varchar(255)) RETURNS INT(10)
  BEGIN
  INSERT INTO sequences (`company_id`, `name`,       `value`)
       VALUES            (company_id, sequence_name, LAST_INSERT_ID(1))
       ON DUPLICATE KEY UPDATE
          `value` = LAST_INSERT_ID(value + 1); 
  RETURN LAST_INSERT_ID(); END
  
  
  CREATE TABLE `sequences` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
    `company_id` int(10) unsigned NOT NULL,
    `value` int(10) unsigned NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `sequences_name_company_id_unique` (`name`,`company_id`),
    KEY `sequences_company_id_index` (`company_id`),
    KEY `sequences_value_index` (`value`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  
  
  //sample output
  MariaDB [testdb]&gt; select version();
  +----------------+
  | version()      |
  +----------------+
  | 10.2.6-MariaDB |
  +----------------+
  1 row in set (0.00 sec)
  
  MariaDB [testdb]&gt; select `getSequenceNumber`(1,'sequence_021');
  +---------------------------------------+
  | `getSequenceNumber`(1,'sequence_021') |
  +---------------------------------------+
  |                                     2 |
  +---------------------------------------+
  1 row in set (0.03 sec)
  
  MariaDB [testdb]&gt; select `getSequenceNumber`(1,'sequence_0212');
  +----------------------------------------+
  | `getSequenceNumber`(1,'sequence_0212') |
  +----------------------------------------+
  |                                      5 |
  +----------------------------------------+
  1 row in set (0.03 sec)
  
  MariaDB [testdb]&gt; select `getSequenceNumber`(1,'new_sequence123');
  +------------------------------------------+
  | `getSequenceNumber`(1,'new_sequence123') |
  +------------------------------------------+
  |                                        6 |
  +------------------------------------------+
  1 row in set (0.03 sec)
  </code></pre>
  
  <p>i have this function my MariaDB and it works, but the problem is when inserting, the new ID it insert is <code>&lt;last_id&gt;</code> + <code>&lt;last value&gt;</code> is there a way to clear/refresh the <code>LAST_INSERT_ID</code> before inserting a record?</p>
  
  <p>EDIT: added create sql statement and sample output</p>