mysql数据库条件批量更新?
有表格:
id datetime youliang_procd
51555 2013-1-18 7:40 173
51556 2013-1-18 7:43 173
51557 2013-1-18 7:50 0
51558 2013-1-18 7:50 0
51572 2013-1-18 8:03 0
51573 2013-1-18 8:04 0
51574 2013-1-18 8:04 0
51575 2013-1-18 8:05 0
51576 2013-1-18 8:11 170
51577 2013-1-18 8:14 170
51578 2013-1-18 8:14 0
51579 2013-1-18 8:15 0
51580 2013-1-18 8:21 167
51581 2013-1-18 8:24 167
51582 2013-1-18 8:30 0
51583 2013-1-18 8:30 0
51584 2013-1-18 8:30 0
问题:若字段youliang_procd=0,将youliang_procd的值更新为上一条记录该字段的值,更新后结果为:
id datetime youliang_procd
51555 2013-1-18 7:40 173
51556 2013-1-18 7:43 173
51557 2013-1-18 7:50 173
51558 2013-1-18 7:50 173
51572 2013-1-18 8:03 173
51573 2013-1-18 8:04 173
51574 2013-1-18 8:04 173
51575 2013-1-18 8:05 173
51576 2013-1-18 8:11 170
51577 2013-1-18 8:14 170
51578 2013-1-18 8:14 170
51579 2013-1-18 8:15 170
51580 2013-1-18 8:21 167
51581 2013-1-18 8:24 167
51582 2013-1-18 8:30 167
51583 2013-1-18 8:30 167
51584 2013-1-18 8:30 167
请问大侠,这条sql语句怎么写(在MySql数据库中),谢谢,在线等!
set @v:=0;<br /> <br /> update tbl_name, (select id, @v:=if(youliang_procd,youliang_procd,@v) as u from tbl_name) t<br /> set tbl_name.youliang_procd=t.u where tbl_name.id=t.id;