所以我想找到平均价格最高的类型,我这样做了:
SELECT MAX(MostExpensive) as HighestPrice, cdTitle
FROM (
SELECT AVG(cdPrice) AS MostExpensive, cdTitle
FROM `cd` GROUP BY cdGenre
) AS MostExpensiveAlbum
然而,我很快意识到我也需要得到艺术家的名字。我可以轻松地在SELECT
语句中添加列,但它会发生,以便艺术家名称列位于另一个表中。
我如何JOIN
我当前正在使用表cd
选择此表artist
并获取artName
列以使其对应于最高的价格。
总之,我想找到平均价格最高的艺术家姓名
以下是我的两个表:
答案 0 :(得分:1)
SELECT MAX(MostExpensive) as HighestPrice, cdTitle,artName
FROM (
SELECT AVG(cd.cdPrice) AS MostExpensive, cd.cdTitle,artist.artname
FROM `cd` inner join 'artist' on cd.artId = artist.artId GROUP BY cdGenre
) AS MostExpensiveAlbum
答案 1 :(得分:1)
试试这个:
SELECT MAX(AvgPrice) As MaximumAveragePrice, ArtName As ArtistName
FROM
(
SELECT AVG(cdPrice) As AvgPrice, ArtName
FROM cd
INNER JOIN artist ON(cd.ArtId = artist.ArtId)
GROUP BY ArtName
) As AvgPricePerArtName
GROUP BY ArtName
HAVING AvgPrice = MAX(AvgPrice)
注意代码直接写在这里,没有测试过。