mysql数据库如何在按某个字段分组下对某一个字段逐个加1

比如表product里有一个字段sort和categoryId,现在sort值都是0,要求在同一categoryId下sort从1递增。

第1个回答  2013-12-20
select a.categoryId,sort+a.n-b.num+1 from 
(
select ROW_NUMBER() over(order by categoryId) n,sort,categoryId from product ) a 
inner join (
select categoryId,min(n) num from (
select ROW_NUMBER() over(order by categoryId) n,sort,categoryId from product )a
group by categoryId
)b on a.categoryId=b.categoryId
order by categoryId