Jump to content

? nested loop to update MySQL rows, using WHILE or IF or BOTH ??


kthibeault1965

Recommended Posts

I need to update rows in a table with serial integer values for each GROUP  For example  1, 2, 3, 4, 5, 6, ... and so on, for each grouping of rows

BEFORE:

GROUP, LINE_NUMBER

100  0

100  0

100  0

200  0

200  0

AFTER:

GROUP,LINE_NUMBER

100  1

100  2

100  3

200  1

200  2

 

 

I have been working  on the solution with no luck, please help if you can

 

$result=mysql_query("select * from csv_stage2 order by parent_id");

$i = 1;

while($row=mysql_fetch_array($result)){

if ($parent_id = $row[parent_id]) {

mysql_query("update csv_stage2 set prod_variant_id = $i where uid = $row[uid]") or die (mysql_error());

 

} else {

$parent_id = $row[parent_id];

$i = 1;

mysql_query("update csv_stage2 set prod_variant_id = 1 where uid = $row[uid]") or die (mysql_error());

}

 

++$i;

}

Link to comment
Share on other sites

I have been working on this version, still no luck tho.

 

$query="select * from csv_stage2 order by parent_id";

$result=mysql_query($query);

$num=mysql_numrows($result);

 

$i=0;

while ($i < $num) {

$id=mysql_result($result,$i,"uid");

$query1="update csv_stage2 set prod_variant_id = $i where uid = $row[uid]";

mysql_query($query) or die(mysql_error());

++$i;

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.