remove values from different rows until data exhaustion?











up vote
2
down vote

favorite












I have a table like this



+----+-------+------+
| id | apple | pear |
+----+-------+------+
| 1 | 3 | 10 |
+----+-------+------+
| 2 | 5 | 8 |
+----+-------+------+
| 3 | 4 | 7 |
+----+-------+------+


I have to delete 9 apple and 11 pears.
A result like this



+----+-------+------+
| id | apple | pear |
+----+-------+------+
| 1 | 0 | 0 |
+----+-------+------+
| 2 | 0 | 7 |
+----+-------+------+
| 3 | 1 | 7 |
+----+-------+------+


Is there a way to do this without having to do a lot of queries?



I did something like this.
I wonder if there is a better way.



    $eat["apple"]=9; 
$eat["pear"]=11;
$id=0; //I will increment this id during the cycle

while ($eat["apple"] > 0 && $eat["pear"] > 0) {
$get_fruit=mysql_query("SELECT id,apple,pear FROM fruits WHERE
id>'".$id."'
ORDER BY id LIMIT 1") or die (mysql_error());

if((mysql_num_rows($get_fruit))==0){
die;
} else {

$fruit = mysql_fetch_assoc($get_fruit);
$id = $fruit["id"];

foreach ($eat as $key => $value) {

if ($fruit[$key]>$eat[$key]) {
$fruit[$key]-=$eat[$key];
$eat[$key]=0;
} else {
$eat[$key]-=$fruit[$key];
$fruit[$key]=0;
}
}

$update=mysql_query("UPDATE fruits SET
apple='".$fruit["apple"]."',
pear='".$fruit["pear"]."'
WHERE id='".$id."'LIMIT 1") or die (mysql_error());
}


}










share|improve this question




















  • 1




    Your apples dont add up.
    – Mihai
    Jan 20 '14 at 14:19










  • UPDATE table SET apple=SUM(apple)-9;UPDATE table SET apple=o WHERE id!=1
    – Mihai
    Jan 20 '14 at 14:20

















up vote
2
down vote

favorite












I have a table like this



+----+-------+------+
| id | apple | pear |
+----+-------+------+
| 1 | 3 | 10 |
+----+-------+------+
| 2 | 5 | 8 |
+----+-------+------+
| 3 | 4 | 7 |
+----+-------+------+


I have to delete 9 apple and 11 pears.
A result like this



+----+-------+------+
| id | apple | pear |
+----+-------+------+
| 1 | 0 | 0 |
+----+-------+------+
| 2 | 0 | 7 |
+----+-------+------+
| 3 | 1 | 7 |
+----+-------+------+


Is there a way to do this without having to do a lot of queries?



I did something like this.
I wonder if there is a better way.



    $eat["apple"]=9; 
$eat["pear"]=11;
$id=0; //I will increment this id during the cycle

while ($eat["apple"] > 0 && $eat["pear"] > 0) {
$get_fruit=mysql_query("SELECT id,apple,pear FROM fruits WHERE
id>'".$id."'
ORDER BY id LIMIT 1") or die (mysql_error());

if((mysql_num_rows($get_fruit))==0){
die;
} else {

$fruit = mysql_fetch_assoc($get_fruit);
$id = $fruit["id"];

foreach ($eat as $key => $value) {

if ($fruit[$key]>$eat[$key]) {
$fruit[$key]-=$eat[$key];
$eat[$key]=0;
} else {
$eat[$key]-=$fruit[$key];
$fruit[$key]=0;
}
}

$update=mysql_query("UPDATE fruits SET
apple='".$fruit["apple"]."',
pear='".$fruit["pear"]."'
WHERE id='".$id."'LIMIT 1") or die (mysql_error());
}


}










share|improve this question




















  • 1




    Your apples dont add up.
    – Mihai
    Jan 20 '14 at 14:19










  • UPDATE table SET apple=SUM(apple)-9;UPDATE table SET apple=o WHERE id!=1
    – Mihai
    Jan 20 '14 at 14:20















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a table like this



+----+-------+------+
| id | apple | pear |
+----+-------+------+
| 1 | 3 | 10 |
+----+-------+------+
| 2 | 5 | 8 |
+----+-------+------+
| 3 | 4 | 7 |
+----+-------+------+


I have to delete 9 apple and 11 pears.
A result like this



+----+-------+------+
| id | apple | pear |
+----+-------+------+
| 1 | 0 | 0 |
+----+-------+------+
| 2 | 0 | 7 |
+----+-------+------+
| 3 | 1 | 7 |
+----+-------+------+


Is there a way to do this without having to do a lot of queries?



I did something like this.
I wonder if there is a better way.



    $eat["apple"]=9; 
$eat["pear"]=11;
$id=0; //I will increment this id during the cycle

while ($eat["apple"] > 0 && $eat["pear"] > 0) {
$get_fruit=mysql_query("SELECT id,apple,pear FROM fruits WHERE
id>'".$id."'
ORDER BY id LIMIT 1") or die (mysql_error());

if((mysql_num_rows($get_fruit))==0){
die;
} else {

$fruit = mysql_fetch_assoc($get_fruit);
$id = $fruit["id"];

foreach ($eat as $key => $value) {

if ($fruit[$key]>$eat[$key]) {
$fruit[$key]-=$eat[$key];
$eat[$key]=0;
} else {
$eat[$key]-=$fruit[$key];
$fruit[$key]=0;
}
}

$update=mysql_query("UPDATE fruits SET
apple='".$fruit["apple"]."',
pear='".$fruit["pear"]."'
WHERE id='".$id."'LIMIT 1") or die (mysql_error());
}


}










share|improve this question















I have a table like this



+----+-------+------+
| id | apple | pear |
+----+-------+------+
| 1 | 3 | 10 |
+----+-------+------+
| 2 | 5 | 8 |
+----+-------+------+
| 3 | 4 | 7 |
+----+-------+------+


I have to delete 9 apple and 11 pears.
A result like this



+----+-------+------+
| id | apple | pear |
+----+-------+------+
| 1 | 0 | 0 |
+----+-------+------+
| 2 | 0 | 7 |
+----+-------+------+
| 3 | 1 | 7 |
+----+-------+------+


Is there a way to do this without having to do a lot of queries?



I did something like this.
I wonder if there is a better way.



    $eat["apple"]=9; 
$eat["pear"]=11;
$id=0; //I will increment this id during the cycle

while ($eat["apple"] > 0 && $eat["pear"] > 0) {
$get_fruit=mysql_query("SELECT id,apple,pear FROM fruits WHERE
id>'".$id."'
ORDER BY id LIMIT 1") or die (mysql_error());

if((mysql_num_rows($get_fruit))==0){
die;
} else {

$fruit = mysql_fetch_assoc($get_fruit);
$id = $fruit["id"];

foreach ($eat as $key => $value) {

if ($fruit[$key]>$eat[$key]) {
$fruit[$key]-=$eat[$key];
$eat[$key]=0;
} else {
$eat[$key]-=$fruit[$key];
$fruit[$key]=0;
}
}

$update=mysql_query("UPDATE fruits SET
apple='".$fruit["apple"]."',
pear='".$fruit["pear"]."'
WHERE id='".$id."'LIMIT 1") or die (mysql_error());
}


}







php mysql rows cycle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 6:34









Cœur

17k9102140




17k9102140










asked Jan 20 '14 at 14:15









Micky

60118




60118








  • 1




    Your apples dont add up.
    – Mihai
    Jan 20 '14 at 14:19










  • UPDATE table SET apple=SUM(apple)-9;UPDATE table SET apple=o WHERE id!=1
    – Mihai
    Jan 20 '14 at 14:20
















  • 1




    Your apples dont add up.
    – Mihai
    Jan 20 '14 at 14:19










  • UPDATE table SET apple=SUM(apple)-9;UPDATE table SET apple=o WHERE id!=1
    – Mihai
    Jan 20 '14 at 14:20










1




1




Your apples dont add up.
– Mihai
Jan 20 '14 at 14:19




Your apples dont add up.
– Mihai
Jan 20 '14 at 14:19












UPDATE table SET apple=SUM(apple)-9;UPDATE table SET apple=o WHERE id!=1
– Mihai
Jan 20 '14 at 14:20






UPDATE table SET apple=SUM(apple)-9;UPDATE table SET apple=o WHERE id!=1
– Mihai
Jan 20 '14 at 14:20














2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










CREATE TABLE t
(`id` int, `apple` int, `pear` int)
;

INSERT INTO t
(`id`, `apple`, `pear`)
VALUES
(1, 3, 10),
(2, 5, 8),
(3, 4, 7)
;

UPDATE t
INNER JOIN (
SELECT
id, apple, pear,
GREATEST(0, apple - GREATEST(0, @atr)) AS new_apples,
@atr := @atr - apple,
GREATEST(0, pear - GREATEST(0, @ptr)) AS new_pears,
@ptr := @ptr - pear
FROM t
, (SELECT @atr := 9, @ptr := 11) variable_initialization_subquery /*@atr = ApplesToRemove...*/
ORDER BY id
) sq ON t.id = sq.id
SET t.apple = sq.new_apples,
t.pear = sq.new_pears;

SELECT * FROM t;

| ID | APPLE | PEAR |
|----|-------|------|
| 1 | 0 | 0 |
| 2 | 0 | 7 |
| 3 | 3 | 7 |



  • DEMO






share|improve this answer




























    up vote
    3
    down vote













    Here's apples...



    SET @apple = 9;
    UPDATE fruits x
    JOIN
    ( SELECT id
    , apple
    , GREATEST(apple-@apple,0)n,@apple:=@apple-apple
    FROM fruits
    ORDER
    BY id
    ) y
    ON y.id = x.id
    SET x.apple = y.n;





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21236266%2fremove-values-from-different-rows-until-data-exhaustion%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      CREATE TABLE t
      (`id` int, `apple` int, `pear` int)
      ;

      INSERT INTO t
      (`id`, `apple`, `pear`)
      VALUES
      (1, 3, 10),
      (2, 5, 8),
      (3, 4, 7)
      ;

      UPDATE t
      INNER JOIN (
      SELECT
      id, apple, pear,
      GREATEST(0, apple - GREATEST(0, @atr)) AS new_apples,
      @atr := @atr - apple,
      GREATEST(0, pear - GREATEST(0, @ptr)) AS new_pears,
      @ptr := @ptr - pear
      FROM t
      , (SELECT @atr := 9, @ptr := 11) variable_initialization_subquery /*@atr = ApplesToRemove...*/
      ORDER BY id
      ) sq ON t.id = sq.id
      SET t.apple = sq.new_apples,
      t.pear = sq.new_pears;

      SELECT * FROM t;

      | ID | APPLE | PEAR |
      |----|-------|------|
      | 1 | 0 | 0 |
      | 2 | 0 | 7 |
      | 3 | 3 | 7 |



      • DEMO






      share|improve this answer

























        up vote
        3
        down vote



        accepted










        CREATE TABLE t
        (`id` int, `apple` int, `pear` int)
        ;

        INSERT INTO t
        (`id`, `apple`, `pear`)
        VALUES
        (1, 3, 10),
        (2, 5, 8),
        (3, 4, 7)
        ;

        UPDATE t
        INNER JOIN (
        SELECT
        id, apple, pear,
        GREATEST(0, apple - GREATEST(0, @atr)) AS new_apples,
        @atr := @atr - apple,
        GREATEST(0, pear - GREATEST(0, @ptr)) AS new_pears,
        @ptr := @ptr - pear
        FROM t
        , (SELECT @atr := 9, @ptr := 11) variable_initialization_subquery /*@atr = ApplesToRemove...*/
        ORDER BY id
        ) sq ON t.id = sq.id
        SET t.apple = sq.new_apples,
        t.pear = sq.new_pears;

        SELECT * FROM t;

        | ID | APPLE | PEAR |
        |----|-------|------|
        | 1 | 0 | 0 |
        | 2 | 0 | 7 |
        | 3 | 3 | 7 |



        • DEMO






        share|improve this answer























          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          CREATE TABLE t
          (`id` int, `apple` int, `pear` int)
          ;

          INSERT INTO t
          (`id`, `apple`, `pear`)
          VALUES
          (1, 3, 10),
          (2, 5, 8),
          (3, 4, 7)
          ;

          UPDATE t
          INNER JOIN (
          SELECT
          id, apple, pear,
          GREATEST(0, apple - GREATEST(0, @atr)) AS new_apples,
          @atr := @atr - apple,
          GREATEST(0, pear - GREATEST(0, @ptr)) AS new_pears,
          @ptr := @ptr - pear
          FROM t
          , (SELECT @atr := 9, @ptr := 11) variable_initialization_subquery /*@atr = ApplesToRemove...*/
          ORDER BY id
          ) sq ON t.id = sq.id
          SET t.apple = sq.new_apples,
          t.pear = sq.new_pears;

          SELECT * FROM t;

          | ID | APPLE | PEAR |
          |----|-------|------|
          | 1 | 0 | 0 |
          | 2 | 0 | 7 |
          | 3 | 3 | 7 |



          • DEMO






          share|improve this answer












          CREATE TABLE t
          (`id` int, `apple` int, `pear` int)
          ;

          INSERT INTO t
          (`id`, `apple`, `pear`)
          VALUES
          (1, 3, 10),
          (2, 5, 8),
          (3, 4, 7)
          ;

          UPDATE t
          INNER JOIN (
          SELECT
          id, apple, pear,
          GREATEST(0, apple - GREATEST(0, @atr)) AS new_apples,
          @atr := @atr - apple,
          GREATEST(0, pear - GREATEST(0, @ptr)) AS new_pears,
          @ptr := @ptr - pear
          FROM t
          , (SELECT @atr := 9, @ptr := 11) variable_initialization_subquery /*@atr = ApplesToRemove...*/
          ORDER BY id
          ) sq ON t.id = sq.id
          SET t.apple = sq.new_apples,
          t.pear = sq.new_pears;

          SELECT * FROM t;

          | ID | APPLE | PEAR |
          |----|-------|------|
          | 1 | 0 | 0 |
          | 2 | 0 | 7 |
          | 3 | 3 | 7 |



          • DEMO







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 20 '14 at 14:55









          fancyPants

          38.5k156378




          38.5k156378
























              up vote
              3
              down vote













              Here's apples...



              SET @apple = 9;
              UPDATE fruits x
              JOIN
              ( SELECT id
              , apple
              , GREATEST(apple-@apple,0)n,@apple:=@apple-apple
              FROM fruits
              ORDER
              BY id
              ) y
              ON y.id = x.id
              SET x.apple = y.n;





              share|improve this answer

























                up vote
                3
                down vote













                Here's apples...



                SET @apple = 9;
                UPDATE fruits x
                JOIN
                ( SELECT id
                , apple
                , GREATEST(apple-@apple,0)n,@apple:=@apple-apple
                FROM fruits
                ORDER
                BY id
                ) y
                ON y.id = x.id
                SET x.apple = y.n;





                share|improve this answer























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Here's apples...



                  SET @apple = 9;
                  UPDATE fruits x
                  JOIN
                  ( SELECT id
                  , apple
                  , GREATEST(apple-@apple,0)n,@apple:=@apple-apple
                  FROM fruits
                  ORDER
                  BY id
                  ) y
                  ON y.id = x.id
                  SET x.apple = y.n;





                  share|improve this answer












                  Here's apples...



                  SET @apple = 9;
                  UPDATE fruits x
                  JOIN
                  ( SELECT id
                  , apple
                  , GREATEST(apple-@apple,0)n,@apple:=@apple-apple
                  FROM fruits
                  ORDER
                  BY id
                  ) y
                  ON y.id = x.id
                  SET x.apple = y.n;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 20 '14 at 14:42









                  Strawberry

                  25.7k83149




                  25.7k83149






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21236266%2fremove-values-from-different-rows-until-data-exhaustion%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine