1

I found the technique to update rows of a table with consecutive numbers here:

Update SQL with consecutive numbering

The gist of the technique is to use (T-SQL):

update myTable
SET @myvar = myField = @myVar + 1

which is awesome and works very well.

Can this technique be extended to number different groups of records, with each group starting with 1 ? e.g.

  • Category 1 rows should get a sequence number 1,2,3,....
  • Category 2 rows should also get a sequence number 1,2,3,.....
Community
  • 1
  • 1
unubar
  • 416
  • 6
  • 15
  • 5
    There's [an answer you want](http://stackoverflow.com/a/1168075/11683) in the question you link to. See the last **EDIT** in it. – GSerg Dec 18 '11 at 11:35
  • It's generally better, if you can generate the sequence after the fact, to always generate it during select rather than storing the sequences. In that way, you don't have to perform maintenance tasks (such as closing gaps during deletes, or reordering values due to updates) – Damien_The_Unbeliever Dec 18 '11 at 13:54
  • possible duplicate of [Update SQL with consecutive numbering](http://stackoverflow.com/questions/1167885/update-sql-with-consecutive-numbering) – gbn Dec 18 '11 at 15:25

1 Answers1

-1

You can simply add a WHERE Clause to your UPDATE statement. see http://dev.mysql.com/doc/refman/5.0/en/update.html for reference.

Basically you do something like:

update myTable
SET @myvar = myField = @myVar + 1
WHERE myCategory = 'CAT'
simonecampora
  • 397
  • 2
  • 8