这是我的表:
+------+---------+------+------+
| Name | orderNo Total used |
+------+---------+------+------+
| Row1 | 1 | 10 | 2 |
| Row2 | 1 | 8 | 1 |
| Row3 | 1 | 7 | 3 |
| Row4 | 2 | 12 | 8 |
+------+---------+-----+-------+
这就是我想要的:
+------+---------+------+------+---------+
| Name | orderNo Total used balance |
+------+---------+------+------+---------+
| Row1 | 1 | 10 | 2 | 8 |
| Row2 | 1 | 8 | 1 | 7 |
| Row3 | 1 | 7 | 3 | 4 |
| Row4 | 2 | 12 | 8 | 4 |
+------+---------+-----+-------+---------+
我从两列的差值计算余额。但无法在总列的下一行中设置余额值。 任何人都可以为我提供解决方案吗?
答案 0 :(得分:0)
我不知道你在哪里遇到问题
SELECT Name, orderNo, Total , used, (Total - used) as balanced
FROM table
如果您想添加强烈不推荐的列:
ALTER TABLE table_name
ADD balanced INT;
然后更新表格:
UPDATE table a
INNER JOIN (SELECT Name, orderNo, Total, used, (Total - used) AS balanced
FROM table) b ON b.Name = a.Name AND b.orderNo = a.orderNo
SET a.balanced = b.balanced