我想从以下select中选择具有最大值的行,但子查询(select max(result.Sum) from result
)中的结果表名称是错误的:
select *
from
(select sum(Rooms.n_seats) as 'Sum', DepKinds.title
from Rooms join Departments on Rooms.department = Departments.id
join DepKinds on Departments.kind = DepKinds.id group by DepKinds.title) result where result.Sum = (select max(result.Sum) from result);
那么如何为这些总和选择最大总和和标题?
答案 0 :(得分:1)
在子查询中无法使用result
后面的FROM
别名。尝试使用HAVING
。
select sum(Rooms.n_seats) as 'Sum', DepKinds.title
from Rooms
join Departments on Rooms.department = Departments.id
join DepKinds on Departments.kind = DepKinds.id
group by DepKinds.title
having sum(Rooms.n_seats) >= all
(
select sum(Rooms.n_seats)
from Rooms
join Departments on Rooms.department = Departments.id
join DepKinds on Departments.kind = DepKinds.id
group by DepKinds.title
)
这将返回具有最多席位数的所有 DepKinds.title
。