title: Get last inserted IDS in MySQL
body: <p>Is it possible to get ALL inserted ID`s after multiple insert  in MySQL by ONE query?
  Example(abstract):</p>
  
  <pre><code>$ids = array();
  $parts = array();
  $query = 'INSERT INTO `TABLENAME` (`FIELDS`)  VALUES'; 
  
  $items = array(values);
  foreach($items as $item){
     $parts[] = '('.$item['key1'].','.$item['key2']...','.$item['keyN'].')';
  }
  
  $query .= implode(',',$parts);
  mysqli_query($link,$query);
  $ids = ... // getting  ALL inserted IDs
  var_dump($ids);
  </code></pre>
  
  <p>It can do so here (one of solutions). Example(abstract):</p>
  
  <pre><code>$ids = array();
  
  $items = array(values);
  foreach($items as $item){
     $query ='INSERT INTO `TABLENAME` (`FIELDS`)  VALUES('.$item['key1'].','.$item['key2']...','.$item['keyN'].')';
     mysqli_query($link,$query);
     $ids[] = mysqli_insert_id($link);
  }
  var_dump($ids);  
  </code></pre>
  
  <p>But, I would like to : one query INSERT all items - one query get ALL inserted ids.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>My solution ( based on question <a href="https://stackoverflow.com/questions/304461/generate-an-integer-sequence-in-mysql">generate an integer sequence in MySQL</a> ).Return sequence of last inserted ID`s by single query. </p>
  
  <pre><code>$query_inserted_ids = "SELECT @row := @row +1 AS row_id
                         FROM TABLENAME , (SELECT @row := LAST_INSERT_ID()-1 )r
                         WHERE @row &lt;(
                         SELECT id
                         FROM  TABLENAME 
                         ORDER BY id DESC
                         LIMIT 1 )";
  </code></pre>
  
  <p>Thanks for attention!</p>