2

我需要在我的数据库中显示最年轻的运动员。MIN(Athlete.Age) 工作正常,但我显然也需要这个名字。当我添加名称以选择时,我得到大约 5 个结果。如何只显示 MIN(Age) 和她的名字?

select MIN(Athlete.Age), Athlete.Name
 from (((Country INNER JOIN Athlete ON Country.Country_Code = Athlete.Country_Code)
  INNER JOIN Athlete_event
   ON Athlete.Athlete_ID = Athlete_event.Athlete_ID2)
  INNER JOIN Event
   ON Event.Event_ID = Athlete_event.Event_ID2)
 Where Athlete.Athlete_ID = Event.Award_Gold
 GROUP BY Athlete.Name;
4

4 回答 4

4

您可能有 5 位同龄的运动员。查询将不知道您想要哪一个。

如果您在数据库中有出生日期,请尝试使用它。

[编辑] 存储年龄通常不是一个好主意,因为在一年内,它们都会出错。

[edit2] 请注意,人们仍然可以共享相同的生日。

于 2012-10-29T15:31:14.233 回答
0

如果您GROUP BY在查询中使用 ,则您正在搜索每个运动员的最小年龄。

您应该尝试使用子查询。将其添加到您的 where 子句中。类似的东西:(未测试)

select Athlete.Age, Athlete.Name
from Athlete
AND Athlete.Age = (select MIN(Athlete.Age)
from (((Country INNER JOIN Athlete ON Country.Country_Code = Athlete.Country_Code)
INNER JOIN Athlete_event
ON Athlete.Athlete_ID = Athlete_event.Athlete_ID2)
INNER JOIN Event
ON Event.Event_ID = Athlete_event.Event_ID2)
Where Athlete.Athlete_ID = Event.Award_Gold);
于 2012-10-29T15:36:10.620 回答
0
select * 
from
(
    select Athlete.Age, Athlete.Name
    from (((Country INNER JOIN Athlete ON Country.Country_Code = Athlete.Country_Code)
      INNER JOIN Athlete_event
       ON Athlete.Athlete_ID = Athlete_event.Athlete_ID2)
      INNER JOIN Event
       ON Event.Event_ID = Athlete_event.Event_ID2)
     Where Athlete.Athlete_ID = Event.Award_Gold  
    order by Age
) v
where rownum=1;
于 2012-10-29T15:28:05.637 回答
0

在您的查询中添加这一行

HAVING MIN(Athlete.Age) =  (SELECT MIN(AGE) from Athlete)

所以你的最终查询看起来像

SELECT MIN(Athlete.Age),
        Athlete.NAME
FROM (((Country INNER JOIN Athlete
            ON Country.Country_Code = Athlete.Country_Code
        ) INNER JOIN Athlete_event
            ON Athlete.Athlete_ID = Athlete_event.Athlete_ID2
        ) INNER JOIN Event
            ON Event.Event_ID = Athlete_event.Event_ID2
        )
WHERE Athlete.Athlete_ID = Event.Award_Gold
GROUP BY Athlete.NAME
HAVING MIN(Athlete.Age) = ( SELECT MIN(AGE) FROM Athlete )
于 2012-10-29T15:28:50.833 回答