update bb set num=(select sum(dd.num2) from aa,bb,cc,dd
 where aa.aid=bb.aid
 and bb.cid=cc.cid and cc.cid=dd.cid
 and aa.id1=1 and cc.flag=1
 group by dd.cid)
 where bb.aid=(select aa.aid from aa,bb,cc,dd
 where bb.cid=cc.cid and cc.cid=dd.cid
 and aa.id1=1 and cc.flag=1)