實現累加的經典sql方法

瓜瓜東西發表於2014-04-09

CREATE TABLE xh(id INT PRIMARY KEY AUTO_INCREMENT,num INT);

 

INSERT INTO xh(num) VALUES(100);

INSERT INTO xh(num) VALUES(200);

INSERT INTO xh(num) VALUES(300);

SELECT * FROM xh

 

SELECT b.id,SUM(a.num) FROM xh a,xh b WHERE b.id>=a.id GROUP BY b.id ORDER BY b.id

 

SELECT b.id,(SELECT SUM(a.num) FROM xh a WHERE a.id<=b.id)FROM xh b;


相關文章