Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP TABLE IF EXISTS tabel;
- CREATE TABLE IF NOT EXISTS tabel(
- id INT AUTO_INCREMENT PRIMARY KEY,
- tgl DATE,
- stok INT
- );
- INSERT INTO tabel(tgl,stok)
- VALUES
- ('2012-08-01',50),
- ('2012-08-02',45),
- ('2012-08-03',40),
- ('2012-08-04',30);
- SELECT * FROM tabel;
- +----+------------+------+
- | id | tgl | stok |
- +----+------------+------+
- | 1 | 2012-08-01 | 50 |
- | 2 | 2012-08-02 | 45 |
- | 3 | 2012-08-03 | 40 |
- | 4 | 2012-08-04 | 30 |
- +----+------------+------+
- SELECT d.id,d.tgl,d.stok,IFNULL(e.stok,d.stok)-d.stok AS keluar
- FROM
- (
- SELECT a.id AS id1, MAX(b.id) AS id2
- FROM
- tabel a
- LEFT JOIN tabel b ON b.id<a.id
- GROUP BY a.id
- ) c
- LEFT JOIN tabel d ON d.id=c.id1
- LEFT JOIN tabel e ON e.id=c.id2;
- +------+------------+------+--------+
- | id | tgl | stok | keluar |
- +------+------------+------+--------+
- | 1 | 2012-08-01 | 50 | 0 |
- | 2 | 2012-08-02 | 45 | 5 |
- | 3 | 2012-08-03 | 40 | 5 |
- | 4 | 2012-08-04 | 30 | 10 |
- +------+------------+------+--------+
- //--
- $sql="SELECT * FROM tabel";
- $result=$db->query($sql);
- $data=array();
- $i=-1;
- foreach($result as $row){
- $data[++$i]=$row;
- $data[$i]['keluar']=($i==0?0:$data[$i-1]['stok']);
- echo "{$data[$i]['id']} | {$data[$i]['tgl']} | {$data[$i]['stok']} | {$data[$i]['keluar']}<br>";
- }
- //--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement