可能是简单的方法,但我不能这样做。我在插入操作中尝试了 sum()
但它不起作用。我有表名称 amount
。
id total_amount 1 200 2 400 3 600 4 800 5 1000
我希望当我在 total_amount
中插入一个值时,它会插入之前的数据并对其求和。
假设如果我想添加 500
总金额,它将插入 1500
。
id total_amount 6 1500
如果我想添加 300
则插入 1800
id total_amount 7 1800
我该怎么做?
最佳答案
您想要将 insert
语句与查询一起使用:
insert into t(id, total_amount)
select max(id)+1, sum(total_amount) + 300
from t;
这也是设置id
的值。如果是auto_increment
,那么就不需要了。
关于php - 插入数据并在列中添加前一行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18106360/