title: Is there a single query that can update a "sequence number" across multiple groups?
body: <p>Given a table like below, is there a single-query way to update the table from this:</p>
  
  <pre><code>| id | type_id | created_at | sequence |
  |----|---------|------------|----------|
  |  1 |       1 | 2010-04-26 | NULL     |
  |  2 |       1 | 2010-04-27 | NULL     |
  |  3 |       2 | 2010-04-28 | NULL     |
  |  4 |       3 | 2010-04-28 | NULL     |
  </code></pre>
  
  <p>To this (note that <code>created_at</code> is used for ordering, and <code>sequence</code> is "grouped" by <code>type_id</code>):</p>
  
  <pre><code>| id | type_id | created_at | sequence |
  |----|---------|------------|----------|
  |  1 |       1 | 2010-04-26 |        1 |
  |  2 |       1 | 2010-04-27 |        2 |
  |  3 |       2 | 2010-04-28 |        1 |
  |  4 |       3 | 2010-04-28 |        1 |
  </code></pre>
  
  <p>I've seen some code before that used an @ variable like the following, that I thought might work:</p>
  
  <pre><code>SET @seq = 0;
  UPDATE `log` SET `sequence` = @seq := @seq + 1
  ORDER BY `created_at`;
  </code></pre>
  
  <p>But that obviously doesn't reset the sequence to 1 for each type_id.</p>
  
  <p>If there's no single-query way to do this, what's the most efficient way?</p>
  
  <p>Data in this table may be deleted, so I'm planning to run a stored procedure after the user is done editing to re-sequence the table.</p>