mySQL查询:SUM / MAX问题

时间:2011-03-16 02:06:55

标签: mysql sum max

我对此非常陌生,并且在我的查询中遇到了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 个答案:

答案 0 :(得分: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样式连接但你应该开始考虑使用它们而不是逗号表示法。