不是表达式初学者组

时间:2014-05-19 18:42:00

标签: sql

这里我有三张桌子

PLAYS {ID_athlete, ID_sport, best_result}

PARTICIPATE {ID_athlete, ID_competition, result}

ISAT {ID_sport, ID_competition}

我想更新播放中的best_result属性。

我写了这个,但我有"不是小组表达"错误。

UPDATE  PLAYS   PL
SET     BEST_RESULT =   
(
    SELECT  MAX(RESULT)
    FROM    PARTICIPATE P
    GROUP BY P.ID_ATHLETE, P.ID_COMPETITION
    HAVING  PL.ID_ATHLETE = P.ID_ATHLETE 
        AND P.ID_COMPETITION IN 
    (
        SELECT  ID_COMPETITION
        FROM    ISAT
        WHERE   ID_SPORT = PL.ID_SPORT
    )
)

我不知道我的错误来自哪里。我希望在某项运动中获得某位运动员的最大成绩并将其置于最佳状态。

2 个答案:

答案 0 :(得分:1)

您的WHEREHAVING条款已经混淆了。对于此SQL(获得最大结果),您甚至不需要GROUP BYHAVING。只是一个条件应该做的工作。

UPDATE PLAYS PL
SET BEST_RESULT =   
    (
    SELECT MAX(RESULT)
    FROM PARTICIPATE P
    WHERE PL.ID_ATHLETE = P.ID_ATHLETE 
          AND P.ID_COMPETITION IN 
                                  (SELECT ID_COMPETITION
                                  FROM ISAT
                                  WHERE ID_SPORT = PL.ID_SPORT)
    )

答案 1 :(得分:1)

我认为你正在寻找这个。使用where子句来限制它并取最大值。 HAVING用于聚合过滤器。

UPDATE  PLAYS   PL
SET     BEST_RESULT =   
(
    SELECT  MAX(RESULT)
    FROM    PARTICIPATE P
    WHERE   PL.ID_ATHLETE = P.ID_ATHLETE 
    AND P.ID_COMPETITION IN 
    (
        SELECT  ID_COMPETITION
        FROM    ISAT
        WHERE   ID_SPORT = PL.ID_SPORT
    )
)