title: How to use auto-increment Id in another column in same INSERT QUERY in MYSQL?
body: <p>How to use auto-increment Id in another column in INSERT QUERY in MYSQL?</p>
  
  <p>I want to add user with code. like if <code>user_id</code> after insert is <code>1</code> the code will be <code>US1</code> similarly if <code>user_id</code> after insert is <code>954</code> the code will be <code>US954</code> </p>
  
  <p>This is what I'm currently doing</p>
  
  <pre><code>"INSERT INTO `users` (`user_id`, `user`, `code`) VALUES (NULL, 'Alice', NULL);"
  </code></pre>
  
  <p>and then retrieve last insert id in PHP (Codeigniter)</p>
  
  <pre><code>$insert_id = $this-&gt;db-&gt;insert_id();
  </code></pre>
  
  <p>and then update code with auto increment id</p>
  
  <pre><code>"UPDATE `users` SET `code` = CONCAT('US', '" . $insert_id . "') WHERE `user_id` = " . $insert_id . ";";
  </code></pre>
  
  <p>Is there anyway you can do it in a single INSERT QUERY in MYSQL?</p>
  
  <p><strong>EDIT:</strong>
  This is not same as this <a href="https://stackoverflow.com/questions/22677344/insert-update-at-the-same-time-in-a-mysql-table">Insert/ update at the same time in a MySql table?</a>. Because this one asking on concurrent operation of two users simultaneously which is not my question</p>