- HTML code
MS SQL2000,表tab1中,数据如下:id stu kind account1 小明 a2 阿东 a3 李四 b4 杨尚 c5 阿强 a6 阿基 b7 要求 a8 文东 a我想实现,按kind统计出每个分类的数量,并更新到account字段,如下:id stu kind account1 小明 a 52 阿东 a 53 李四 b 24 杨尚 c 15 阿强 a 56 阿基 b 27 要求 a 58 文东 a 5请问这个更新的SQL语句怎么写呢?在线等。。。。
------解决方案--------------------
- SQL code
update aset a.account =b.countsfrom tab1 a, (select kind,count(1)as counts from tab1 group by kind)bwhere a.kind=b.kind
------解决方案--------------------
- SQL code
update tab1 set account=(select count(1) from tab1 where kind=t.kind) from tab1 t