I was trying to select the Band with the highest Albums number, as an error it shows
Unknown column 'N_Albums' in 'field list'
What shoud I do in order to see the name of the band and its albums number?
select Name, max(N_Abums) as `Albums`
from (select a.Name, count(b.CodDisc) as `Nr`
from `S8.Band` a join `S8.Album` b
where a.CodBand = b.CodBand
group by a.CodBand) as Test
Order the result accordingly and take only the first record
select a.Name, count(b.CodDisc) as `Nr`
from S8.Band a
join S8.Album b on a.CodBand = b.CodBand
group by a.CodBand, a.Name
order by count(b.CodDisc) desc
limit 1
不需要MAX
,只需ORDER BY <COLUMN> DESC
并获得第一行。
Change N_Albums
to Nr
.
You aliased the count with Nr
, so that's what you have to take the max() of.
select Name, max(Nr) as Albums
from (
select a.Name, count(b.CodDisc) as Nr
from S8.Band a
join S8.Album b on a.CodBand = b.CodBand
group by a.CodBand
) as Test
I also improved your join syntax and removed all backticks as they were unnecessary.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.