mysql:正在获取具有最大列值的别名的元组

mysql: getting getting tuple of an alias which has maximum column value

我想做的是:

select AlbumId from (
    select Album.AlbumId, Sum(Track.UnitPrice) as Total from Album 
    inner join Track on Album.AlbumId = Track.AlbumId
    group by Album.AlbumId
) tpt 
where Total = (select max(Total) from tpt);

但我得到:

Table 'db.tpt' doesn't exist

-这可能是有道理的。但是,我是 SQL 的新手,不知道为什么 tpt 在我的 where 子句中超出了范围。我怎样才能做到这一点?我想获得最大总数的元组并提取 AlbumId.

谢谢!

对子查询使用 JOIN

select tpt.AlbumId from (
    select Album.AlbumId, Sum(Track.UnitPrice) as Total from Album 
    inner join Track on Album.AlbumId = Track.AlbumId
    group by Album.AlbumId
) tpt 
JOIN (
    select Sum(Track.UnitPrice) as Total from Album 
    inner join Track on Album.AlbumId = Track.AlbumId
    group by Album.AlbumId
    ORDER BY Total DESC
    LIMIT 1
) maxtpt ON tpt.Total = maxtpt.Total