Mysql database condition batch update? Table: id & nbsp; datetime & nbsp; youliang_procd & nbsp; 51555 & nbsp; 2013-1-18 & nbsp; 173 & nbsp; 51556 & nbsp; 2013-1-18 & nbsp; & nbsp; 173 & nbs mysql database condition batch update?
Table:
Id datetime youliang_procd
51555 173
51556 173
51557 0
51558 0
51572
51573 0
51574 0
51575
51576 170
51577 170
51578
51579
51580 167
51581 167
51582
51583
51584
Problem: If the field youliang_procd = 0, update the youliang_procd value to the value of the field recorded in the previous record. the updated result is:
Id datetime youliang_procd
51555 173
51556 173
51557 173
51558 173
51572 173
51573 173
51574 173
51575 173
51576 170
51577 170
51578 170
51579 170
51580 167
51581 167
51582 167
51583 167
51584 167
Could you tell me how to write this SQL statement (in MySql database)? Thank you, online, etc!
------ Solution --------------------
This must be completed using SQL. it is more convenient to use code.
------ Solution --------------------
You can use SQL to directly complete
set @v:=0;
update tbl_name, (select id, @v:=if(youliang_procd,youliang_procd,@v) as u from tbl_name) t
set tbl_name.youliang_procd=t.u where tbl_name.id=t.id;
Two commands in total, which must be executed separately in php
Where
Set @ v: = 0 is used to declare a user variable.
Select id, @ v: = if (youliang_procd, youliang_procd, @ v) as u from tbl_name is used to bring the non-empty youliang_procd field value to the next
Tbl_name name of the table to be operated
------ Solution --------------------
SQL is completed directly ??
------ Solution --------------------
Reference:
SQL is completed directly ??
Yes!
If php is used, you need to read, judge, and modify data one by one.
This will be a very painful process