【发布时间】:2014-12-07 06:46:43
【问题描述】:
我必须编写一个查询,该查询使用一个变量列出所有交易,按日期排序,由 ID 为 1 的客户执行。该变量应保持运行余额,以显示每次交易后客户的新余额。最终输出应给出交易的日期、金额和交易后的当前余额。
表格
+--------------+------------------+------+-----+-------------------+-----------------------------+
| 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;
【问题讨论】:
标签: mysql