Mysql database condition batch update? What should I do?

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.