Ok, I approached the problem in a different way and finally reached the desired outcome. Here's the code if anyone ever gets stuck with this same problem. The way I solved is by using 'dynamic' views instead of loops. I'm not sure whether it's the most elegant way of doing things, but I'm still learning and it worked for me, so hey!
1) First thing is to create the table
CREATE TABLE table2 (
DateT date DEFAULT NULL,
HTeam varchar(255) DEFAULT NULL,
ATeam varchar(255) DEFAULT NULL,
HScore int(10) UNSIGNED DEFAULT NULL,
AScore int(10) UNSIGNED DEFAULT NULL
) ENGINE = INNODB;
2) Populate with data
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-01', 'T1', 'T2', 1, 0);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-01', 'T3', 'T4', 2, 1);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-08', 'T2', 'T3', 2, 2);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-08', 'T4', 'T1', 0, 2);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-15', 'T1', 'T3', 3, 1);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-15', 'T2', 'T4', 1, 2);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-22', 'T4', 'T2', 3, 2);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-22', 'T3', 'T1', 0, 0);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-29', 'T1', 'T4', 1, 1);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-03-29', 'T3', 'T2', 4, 2);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-04-05', 'T2', 'T1', 0, 2);
INSERT INTO test.table2(DateT, HTeam, ATeam, HScore, AScore) VALUES
('2014-04-05', 'T4', 'T3', 3, 1);
3) Next, I created a few functions that I can later use in my views to make them act in a 'dynamic' way. This is a little trick I came across on this website:
DROP FUNCTION IF EXISTS func_hteam;
CREATE FUNCTION func_hteam() RETURNS VARCHAR(255) DETERMINISTIC
RETURN @hteam;
DROP FUNCTION IF EXISTS func_ateam;
CREATE FUNCTION func_ateam() RETURNS VARCHAR(255) DETERMINISTIC
RETURN @ateam;
DROP FUNCTION IF EXISTS func_datet;
CREATE FUNCTION func_datet() RETURNS date DETERMINISTIC
RETURN @datet;
4) Next, I created a 'dynamic' view:
DROP VIEW IF EXISTS view_total;
CREATE VIEW view_total (DateT, HTeam, ATeam, HScore, AScore, Total)
AS
(SELECT DateT, HTeam, ATeam, HScore, AScore, (HScore + AScore) AS 'Sum' FROM test.table2
WHERE (HTeam = func_hteam() OR ATeam = func_hteam()) AND DateT < func_datet()
ORDER BY DateT DESC LIMIT 4)
UNION DISTINCT
(SELECT DateT, HTeam, ATeam, HScore, AScore, (HScore + AScore) AS 'Sum' FROM test.table2
WHERE (HTeam = func_ateam() OR ATeam = func_ateam()) AND DateT < func_datet()
ORDER BY DateT DESC LIMIT 4)
ORDER BY DateT DESC;
5) Next, I created a function that calculates the total sum of all rows that the previous view returns:
DROP FUNCTION IF EXISTS func_total;
DELIMITER $$
CREATE FUNCTION func_total() RETURNS int DETERMINISTIC
BEGIN
DECLARE num int UNSIGNED DEFAULT 0;
SELECT SUM(total) INTO num FROM view_total;
RETURN (num);
END
$$
6) Finally, the function that makes all this gel together:
DROP FUNCTION IF EXISTS func_final;
DELIMITER $$
CREATE FUNCTION func_final(hteam VARCHAR(255), ateam VARCHAR(255), datet DATE) RETURNS int DETERMINISTIC
BEGIN
DECLARE total INT DEFAULT 0;
SELECT func_total() INTO total
FROM (SELECT @hteam:=hteam hteam,
@ateam:=ateam ateam,
@datet:=datet datet) t1, view_total t
ORDER BY t.datet ASC LIMIT 1;
RETURN (total);
END
$$
7) ...and now we get the data out:
SELECT DateT, HTeam, ATeam, HScore, AScore, func_final(HTeam, ATeam, Datet) AS TOTAL
FROM table2 ORDER BY DateT DESC;
All works, could this have been written in a nicer way? Maybe, but it's beyond my abilities at this stage :)
Good day! :P