我的数据库名称是test,我有一个名为HaveGoal的表。 我在问这个:
SELECT Rel.total
FROM(SELECT H.number,H.teamname,SUM(H.numberofgoals)AS总
来自HaveGoal H GROUP BY H.number,H.teamname)AS Rel
WHERE Rel.total =(SELECT MAX(Rel.total)FROM Rel)
它给出:ERROR 1146(42S02):表' test.Rel'不存在
答案 0 :(得分:0)
似乎最后一个subselect不能引用FROM
子句中定义的嵌套查询。
在这种情况下,您有多种解决方案:
WITH
声明复制适用于任何情况:
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(Rel2.total)
FROM (
SELECT H.number, H.teamname, SUM(H.numberofgoals) AS total
FROM HaveGoal H
GROUP BY H.number,H.teamname
) AS Rel2
)
排序后的第一行要短得多,但隐含MAX
:
SELECT Rel.total
FROM (
SELECT H.number, H.teamname, SUM(H.numberofgoals) AS total
FROM HaveGoalTest H
GROUP BY H.number,H.teamname
) AS Rel
ORDER BY total DESC
LIMIT 1