2011-03-16 82 views
0

我很新的這一點,在我的查詢的SUM和MAX有問題:的MySQL查詢:SUM/MAX問題

SELECT Score.performerId, Performer.performerName, 
Performer.countrycode, Country.countryName, Event.eventName, Score.points 
FROM Score, Performer, Country, Event 
WHERE points = (SELECT MAX(points) FROM Score 
    WHERE roundName = 'Final') 
AND roundName = 'Final' 
AND Score.performerId=Performer.performerId 
AND Performer.countryCode=Country.countryCode 
AND Score.eventId=Event.eventId 

,我將不勝感激任何幫助。

回答

1
SELECT 
    Score.performerId, Performer.performerName, 
    Performer.countrycode, Country.countryName, Event.eventName, Score.points 
FROM Score, Performer, Country, Event, 
    (
    SELECT Performer.PerformerId, SUM(score.Points) TotalScore 
    FROM Score, Performer, Country, Event 
    WHERE roundName = 'Final' 
    AND Score.performerId=Performer.performerId 
    AND Performer.countryCode=Country.countryCode 
    AND Score.eventId=Event.eventId 
    group by Performer.PerformerId 
    order by TotalScore Desc 
    LIMIT 1 
    ) MaxScore 
WHERE roundName = 'Final' 
AND Score.performerId=Performer.performerId 
AND Performer.countryCode=Country.countryCode 
AND Score.eventId=Event.eventId 
AND Performer.PerformerId=MaxScore.PerformerId 

我有點懶轉換爲ANSI風格的加入,但你應該開始尋找優先於逗號符號使用它們。