My database name is test, I have a table named HaveGoal. I am querying this:
SELECT Rel.total
FROM (SELECT H.number, H.teamname, SUM(H.numberofgoals) AS total
FROM HaveGoal H GROUP BY H.number,H.teamname) AS Rel
WHERE Rel.total = (SELECT MAX(Rel.total) FROM Rel)
It gives:ERROR 1146 (42S02): Table 'test.Rel' doesn't exist
It seems that the last subselect cannot reference a nested query defined in the
FROMclause.In this case you have multiple solutions :
WITHstatementDuplicating will work in any situation :
Taking the first line after sorting is much shorter, but the
MAXis implied :