Question

I am creating a simple PHP webpage with MySQL database. The MYSQL database have the following contents: (illustration on left) I just want to know a possible way of doing some Data Manipulation to achieve the results on the right portion below.

 (initial value)                                                 (final result)

    ID | MINUS '1'(to "next" ID's)                ID |               ID |

     1 |   0                                       1 | ------------>  1 | 
     2 |   1 --> (will minus '1' to ID 3           2 | ------------>  2 | 
     3 |   0     up to the last)            3-1 =  2 | ------------>  2 | 
     4 |   0                                4-1 =  3 | ------------>  3 | 
     5 |   1 --> (minus '1' AGAIN to ID 6   5-1 =  4 | ------------>  4 | 
     6 |   0      up to the last)           6-1 =  5      -1    --->  4 | 
     7 |   0                                7-1 =  6      -1    --->  5 | 

It is just COUNTING the number of minus and then subtracting it to ALL the ID that follows it. ID 2 has tells the next ID, which is 3 to subtract 1 to itself up to the last ID, (3-1 , 4-1, 5-1, 6-1, 7-1 ) but another instance came. ID 5 tells ID 6 to subtract itself again up to the last, so MINUS 2 from ID 6 up to the last ID. from just 6-1, 7-1 ---> 6-2, 7-2.

I know it sounds very easy for you, but I'm just a newbie and find this thing hard. Sorry for the headache. Hope someone helps, Thanks!

my DML is like (just for illustration, this should be in php, I will convert it, just help me in the LOGIC)

for($x=0;$x<num_rows;$x++)
   {

   if(MINUS = 1){
      query(UPDATE table_name SET ID=ID-1 WHERE ID = $x);}
    }

Something like that, i am still a newbie in decision making loops. I will be grateful if you could fix my loop. I am new to this community, I beg for your understanding.

Was it helpful?

Solution

You can do this in MySQL either using variables or standard SQL. Here is the standard SQL:

select id, minus,
       (id - coalesce((select sum(minus) from table t2 where t2.id < t.id), 0)) as newId
from table t;

Here is the variable version:

select id, minus,
       (id - minus + (@value := @value + minus)) as newId
from table t cross join
     (select @value = 0) const
order by id

OTHER TIPS

You can use a MySQL implementation as follows:

select id, minus,
   (id - coalesce((select sum(minus) from table t2 where t2.id < t.id), 0)) as newId

from table t;

The code idea came from Gordon's post, here is an elaboration.

The first part is select which states that you want to select certain input from mysql. id, minus follows, and means that you want to return the values of id and minus when the query has finished running.

The rest of it falls under the third part, where you perform the subtraction, and return the value of the subtraction as newId. So when the sql returns, it will get a row with ID, MINUS and NEWID as it's headings.

The subtraction works like this: coalesce returns the first non-null value (i.e. 1) in the list. the table t2 is using a second version of the table and calling it t2 so that you can compare information.

I hope this clarified things for you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top