根据某个字段值不同修改另一个字段自加

时间:2010-04-15 20:25 作者:mysql问答 点击:
很多时候我们需要修改某个字段自加。根据某个条件!如案例

  

很多时候我们需要修改某个字段自加。根据某个条件!如案例

phpzy.com 是表

id aid title
1   1     a
2   1     a
3   1     b
4   1     b
5   1     b

修改成
id aid title
1    1    a
2    2    a
3    1    b
4     2   b
5     3   b

 

update phpzy.com x ,(
select id, (select count(*) from 表a where title= t.title and id<=t.id) as cnt
from phpzy.com t) y
set x.aid=y.cnt
where x.id=y.id

当title不同时
aid重1开始递增

如果一条语句不行可以写成
where title='a'和where title='b'两条实现也行
 


标签(Tag):修改sql自加 修改sql自增 修改sql相加
------分隔线----------------------------
推荐内容
热点内容