Mysql 如何保持交易的连续余额,并在交易后获得金额
我必须编写一个查询,使用一个变量列出ID为1的客户执行的按日期排序的所有交易。变量应保持一个运行余额,以显示每次交易后客户的新余额。最终输出应给出日期、交易金额和交易后的当前余额 表格Mysql 如何保持交易的连续余额,并在交易后获得金额,mysql,Mysql,我必须编写一个查询,使用一个变量列出ID为1的客户执行的按日期排序的所有交易。变量应保持一个运行余额,以显示每次交易后客户的新余额。最终输出应给出日期、交易金额和交易后的当前余额 表格 +--------------+------------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default
+--------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+-------------------+-----------------------------+
| customer_id | int(10) unsigned | YES | MUL | NULL | |
| last_created | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| amount | decimal(6,2) | NO | | NULL | |
+--------------+------------------+------+-----+-------------------+-----------------------------+
查询
SELECT last_created, amount , sum(amount) as moneyspent FROM transactions where
customer_id = 1 ORDER BY last_created;
SET @total = 0;
SELECT last_transaction, amount ,@total := @total + amount AS runningtotal
FROM sakila_payment
ORDER BY last_transaction;