DELIMITER $$
DROP PROCEDURE IF EXISTS `get_totaltab`$$
CREATE PROCEDURE `get_totaltab`()
BEGIN
declare i int default 1;
while i < 5
do
INSERT INTO book_copy SELECT max(id),name,sum(money),max(time) from book where name=(SELECT name FROM book where id=i) and id<=i;
set i = i + 1;
end while;
END$$
DELIMITER ;
CALL get_totaltab()
This is the result, as shown below
1 mike 6 2016-09-01
2 mike 654 2016-09-01
3 leo 488 2016-09-02
4 mike 660 2016-09-03
At present, I have also thought of a way. I don’t know if there is a better way to compare and learn from each other
Thank you for the invitation, try this~
Thanks for the invitation. Do you want to accumulate the amount field? Isn’t it possible to use user IDs for conditional accumulation?
You can take a look at the union keyword in sql
I feel that sql is not good at doing this kind of thing, and it would be better to put it in the code.
If there is one record per user, then
group by + sumis enough.This is a stored procedure
This is the result, as shown below