日期:2014-05-16 浏览次数:20820 次
mysql> select * from `凭证表`; +-----------+--------+--------+--------+--------+ | 日期 | 凭证字 | 凭证号 | 借金额 | 贷金额 | +-----------+--------+--------+--------+--------+ | 2006-9-13 | 转 | 1 | ? | ? | | 2006-9-14 | 付 | 1 | ? | ? | +-----------+--------+--------+--------+--------+ 2 rows in set (0.00 sec) mysql> select * from `凭证明细表`; +------------+--------+--------+------+----------+----------+ | 日期 日期 | 凭证字 | 凭证号 | 序号 | 借贷方向 | 发生金额 | +------------+--------+--------+------+----------+----------+ | 2006-9-13 | 转 | 1 | 1 | 借 | 100 | | 2006-9-13 | 转 | 1 | 2 | 借 | 100 | | 2006-9-13 | 转 | 1 | 3 | 贷 | 200 | | 2006-9-14 | 付 | 1 | 1 | 借 | 300 | | 2006-9-14 | 付 | 1 | 2 | 贷 | 300 | +------------+--------+--------+------+----------+----------+ 5 rows in set (0.00 sec) mysql> update `凭证表` as a -> set `借金额` = (select sum(`发生金额`) from `凭证明细表` as b where a.`日 期` = b.`日期` and `借贷方向` = '借'), -> `贷金额` = (select sum(`发生金额`) from `凭证明细表` as b where a.`日 期` = b.`日期` and `借贷方向` = '贷'); Query OK, 2 rows affected (0.03 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from `凭证表`; +-----------+--------+--------+--------+--------+ ; | 日期 | 凭证字 | 凭证号 | 借金额 | 贷金额 | +-----------+--------+--------+--------+--------+ | 2006-9-13 | 转 | 1 | 200 | 200 | | 2006-9-14 | 付 | 1 | 300 | 300 | +-----------+--------+--------+--------+--------+ 2 rows in set (0.00 sec)