Update several rows with different values SQL
UPDATE table
SET
amount -= '$amount'
WHERE
type = '1' AND
amount - '$amount' >= '0'
Okay, let´s explain. If I have two rows in my table:
type | amount
1 | 30
1 | 20
Altogether I want to subtract whatever $amount
is, from rows where type
is equal to 1
. 开发者_C百科So if $amount
holds number 40, that means that I altogether want to subtract 40 and get this result:
type | amount
1 | 0
1 | 10
(30 from row 1 and 10 from row 2, that means 40 has been subtracted)
So if one row doesn't cover the number in $amount
I want to continue subtracting on another row. But if not even every row together cover $amount
, no subtracting shall be made.
Which is the easiest way to manage this?
I use PHPMyAdmin.
Thanks for your help!
If I understand what your trying to do, it is too complicated for the SQL language: or at least too complicated to be practical.
As I see it you have two options that I can think of:
1) You can either retrieve all the rows with id=1 from the database and in PHP modify them accordingly and update each of them afterward. This will be the easiest.
2) Create a user defined function in your database that does the processing. This will be the safest and most efficient but difficult to implement depending on your database.
Something like this (assuming MySQL) would be the beginnings on it. You'd need to use a variable to keep track of how much of the original amount is still available. This'll need some refinement to handle edge cases (if any), and I'm going off the top of my head, so most likely won't work at all (my brain is mush today):
select @available := 40;
UPDATE yourtable
SET @newavailable := amount - @available, amount = amount - @available, @available := @newavailable
WHERE @available > 0;
You can use a stored procedure to do this, it would look something like this:
SET @totalamount = 40; (x amount 40 is example)
IF @totalamount - (select sum(amount) from table where type =1) < 0 THEN return;
ELSE @count = 0; WHILE(@totalamount > 0) DO
SET @recordamount = SELECT amount FROM table ORDER BY somevalue limit @count,1;
SET @identifier = SELECT primarykey FROM table ORDER BY somevalue limit @count,1;
@totalamount - @recordamount;
IF(@totalamount < 0)
UPDATE table SET amount = -@totalamount WHERE primarykeycolumn = @identifier;
ELSE
UPDATE table SET amount = 0 WHERE primarykeycolumn = @identifier;
END IF;
@count = @count + 1;
END WHILE; END IF;
You will need to choose a variable to order the table by which will determine what order the calculation will be performed in, and you need a primary key to identify records. This is definitely a terrible way to do this and it would be a better idea to do it programatically by loading results of the SQL query into an array. If there's a really good reason to keep everything in SQL this, probably with some modification? will do it.
精彩评论