34,590
社区成员
发帖
与我相关
我的任务
分享
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Uid] int,[SortIndex] int)
insert [tb]
select 396028,0 union all
select 396028,0 union all
select 396028,0 union all
select 214014,0 union all
select 396028,0
go
alter table tb add tmpid int identity(1,1);
go
update t
set SortIndex=(select count(1)+1 from tb where uid=t.uid and tmpid<t.tmpid)
from tb t
go
alter table tb drop column tmpid;
go
select * from tb
/**
Uid SortIndex
----------- -----------
396028 1
396028 2
396028 3
214014 1
396028 4
(5 行受影响)
**/