简体   繁体   English

用不同的值更新几行 SQL

[英]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 .总之,我想从type等于1的行中减去$amount So if $amount holds number 40, that means that I altogether want to subtract 40 and get this result:所以如果$amount持有数字 40,这意味着我完全想减去 40 并得到这个结果:

type | amount    
1    | 0    
1    | 10

(30 from row 1 and 10 from row 2, that means 40 has been subtracted) (第 1 行中的 30 和第 2 行中的 10,这意味着已减去 40)

So if one row doesn't cover the number in $amount I want to continue subtracting on another row.因此,如果一行不包含$amount中的数字,我想继续减去另一行。 But if not even every row together cover $amount , no subtracting shall be made.但如果不是每一行都覆盖$amount ,则不应进行减法。

Which is the easiest way to manage this?管理这个最简单的方法是什么?

I use PHPMyAdmin.我使用 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.如果我理解您要做什么,对于 SQL 语言来说太复杂了:或者至少太复杂而不实用。
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. 1) 您可以从数据库中检索所有 id=1 的行,然后在 PHP 中相应地修改它们,然后更新它们中的每一个。 This will be the easiest.这将是最简单的。

2) Create a user defined function in your database that does the processing. 2)在您的数据库中创建一个用户定义的 function 进行处理。 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.像这样的东西(假设 MySQL)将是它的开始。 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;设置@totalamount = 40; (x amount 40 is example) (x 金额 40 为示例)

IF @totalamount - (select sum(amount) from table where type =1) < 0 THEN return; IF @totalamount - (从类型 =1 的表中选择 sum(amount)) < 0 THEN return;

ELSE @count = 0;否则@count = 0; WHILE(@totalamount > 0) DO WHILE(@totalamount > 0) 做

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.这绝对是一种糟糕的方法,最好通过将 SQL 查询的结果加载到数组中以编程方式进行。 If there's a really good reason to keep everything in SQL this, probably with some modification?如果有一个非常好的理由将所有内容保留在 SQL 中,可能需要进行一些修改? will do it.会做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM