简体   繁体   English

使用组中的连续编号更新SQL

[英]Update SQL with consecutive numbering within a group

I found the technique to update rows of a table with consecutive numbers here: 我发现了一种在这里用连续数字更新表行的技术:

Update SQL with consecutive numbering 使用连续编号更新SQL

The gist of the technique is to use (T-SQL): 该技术的要旨是使用(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 ? 可以将此技术扩展到为不同的记录组编号,每组记录从1开始吗? eg 例如

  • Category 1 rows should get a sequence number 1,2,3,.... 类别1行应获得序列号1,2,3,....
  • Category 2 rows should also get a sequence number 1,2,3,..... 类别2行还应获得序列号1,2,3,.....

You can simply add a WHERE Clause to your UPDATE statement. 您可以简单地将WHERE子句添加到UPDATE语句中。 see http://dev.mysql.com/doc/refman/5.0/en/update.html for reference. 请参阅http://dev.mysql.com/doc/refman/5.0/en/update.html以获取参考。

Basically you do something like: 基本上,您可以执行以下操作:

update myTable
SET @myvar = myField = @myVar + 1
WHERE myCategory = 'CAT'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM