I am having this problem that I just can't figure out and I was wondering if anyone had any information on it.
I am currently running the below SQL (In a schema similar to here: demo)...
SELECT match_id, team, date, hometeam, awayteam, FTHG, FTAG, `2.5 goals`
FROM (
SELECT
match_id, team, TEMP.date, hometeam, awayteam, FTHG, FTAG,
@teamCounter:=IF(@prevHome=team,@teamCounter+1,1) teamCounter,
@prevHome:=team,
CASE WHEN FTHG + FTAG < 2.5 THEN 'UNDER'
ELSE 'OVER'
END AS `2.5 goals`
FROM TEMP
JOIN (
SELECT DISTINCT date, hometeam team
FROM TEMP
UNION
SELECT DISTINCT date, awayteam
FROM TEMP
ORDER BY team, date DESC
) allgames
ON TEMP.date = allgames.date
AND (TEMP.hometeam = allgames.team
OR TEMP.awayteam = allgames.team)
JOIN (SELECT @teamCounter:=0) t
ORDER BY team, TEMP.date DESC
) t
WHERE teamCounter <= 6
ORDER BY team, date;
The SQL is based on a Stackoverflow answer I found Here, but basically I want it to show me the last 6 games that each team was involved in (either home or away).
The strange thing is, when I run this in MySQL, it will work in the first SQL session, but sessions created after that it will return vastly different results. I can then remove the match_id field from the outer SELECT list and the query will return to it's normal dataset, until another session is opened again... and then returning the match_id field will show the desired results as to begin with, starting us at the beginning again.
I'm not sure if the SQLFiddle I have linked to will recreate the problem but can anyone explain why this is happening?
Any help greatly appreciated!
Kev