1
我有一個MySQL表,我在表中顯示借方,貸方和餘額。我已經裝瞭如下定義,樣本數據和代碼到SQL Fiddle:將期初餘額添加到MySQL的第一個期末餘額行中
CREATE TABLE chequebook (
entry_date timestamp default now() PRIMARY KEY,
entry_item varchar(48) NOT NULL DEFAULT '',
entry_amount decimal(10,2) NOT NULL DEFAULT 0.00
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO chequebook (entry_date,entry_item,entry_amount) VALUES
('2010-01-02 12:34:00','Deposit A',215.56),
('2010-01-02 21:44:00','Withdrawal A' ,-23.34),
('2010-01-03 10:44:00','Withdrawal B',-150.15),
('2010-01-03 15:44:00','Deposit B',154.67),
('2010-01-04 18:44:00','Withdrawal C',-65.09),
('2010-01-05 08:44:00','Withdrawal D',-74.23),
('2010-01-06 14:44:00','Deposit C',325.12),
('2010-01-06 20:44:00','Withdrawal E',-80.12),
('2010-01-07 04:44:00','Withdrawal F',-110.34),
('2010-01-07 16:44:00','Withdrawal G',-150.25),
('2010-01-08 16:44:00','Withdrawal H',-23.90),
('2010-01-08 21:44:00','Withdrawal I',-75.66),
('2010-01-08 22:44:00','Deposit C',275.78),
('2010-01-09 11:44:00','Withdrawal K',-85.99),
('2010-01-09 21:44:00','Withdrawal J',-100.00);
set @depos=0;
set @total=0;
select
entry_date,
entry_item,
entry_amount,
if(entry_amount>0, @depos:=entry_amount, @depos:[email protected]+entry_amount) as depos_bal,
@total:[email protected]+entry_amount as net_bal
from chequebook
order by entry_date;
我面臨的問題時,我想的期初餘額增加從PHP MYSQL查詢net_bal
列。
我面臨的問題是將開盤餘額添加到第一列,然後它應該減去或加上所需字段。
例如:
| entry_date | entry_item | entry_amount | depos_bal | net_bal |
|---------------------------|--------------|--------------|-----------|---------|
| January, 02 2010 12:34:00 | Deposit A | 215.56 | 5215.56 | 5215.56 | <--- 5000 is openingbalance
| January, 02 2010 21:44:00 | Withdrawal A | -23.34 | 5192.22 | 5192.22 |
| January, 03 2010 10:44:00 | Withdrawal B | -150.15 | 5042.07 | 5042.07 |
打開平衡不同於表提取。
我該怎麼做?
完美,非常感謝!我不明白爲什麼我沒有想到這一點。我正在試圖處理查詢。從來沒有在你的解決方案中猜出它。再次感謝。 – sammry