我想通过SQL根据另一列从两个表中选择两个不同的列

时间:2014-08-11 20:15:03

标签: sql jdbc oracle10g

我真的遇到了这个问题:我有一个来自Oracle 10g XE数据库的两个表。我被要求从活动中给出最快的男女运动员的FName和LName。将发出eventID。它的工作方式就像某人询问事件ID一样,顶级男性和女性的FName和LName将分别发出。

我应该指出,每位运动员都会有独特的表现记录。谢谢你提醒!

这是两张桌子。我花了整整一夜的时间。

ATHLETE

╔═════════════════════════════════════════╦══╦══╗
║ athleteID* FName    LName    Sex   Club ║  ║  ║
╠═════════════════════════════════════════╬══╬══╣
║ 1034       Gabriel  Castillo M     5011 ║  ║  ║
║ 1094       Stewart  Mitchell M     5014 ║  ║  ║
║ 1161       Rickey   McDaniel M     5014 ║  ║  ║
║ 1285       Marilyn  Little   F          ║  ║  ║
║ 1328       Bernard  Lamb     M     5014 ║  ║  ║
║                                         ║  ║  ║
╚═════════════════════════════════════════╩══╩══╝

PARTICIPATION_IND

╔═════════════════════════════════════════════╦══╦══╗
║ athleteID* eventID* Performance_in _Minutes ║  ║  ║
╠═════════════════════════════════════════════╬══╬══╣
║ 1094       13       18                      ║  ║  ║
║ 1523       13       17                      ║  ║  ║
║ 1740       13                               ║  ║  ║
║ 1285       13       21                      ║  ║  ║
║ 1439       13       25                      ║  ║  ║
╚═════════════════════════════════════════════╩══╩══╝

4 个答案:

答案 0 :(得分:1)

SELECT * FROM (
    SELECT 
        a.FName
        , a.LName 
        FROM ATHLETE a
    JOIN PARTICIPATION_IND p 
    ON a.athleteID = p.athleteID
    WHERE a.Sex = 'M'
    AND p.eventID = 13
    ORDER BY p.Performance_in_Minutes
)
WHERE ROWNUM = 1
UNION
SELECT * FROM
    SELECT (
        a.FName
        , a.LName 
    FROM ATHLETE a
    JOIN PARTICIPATION_IND p 
    ON a.athleteID = p.athleteID
    WHERE a.Sex = 'F'
    AND p.eventID = 13
    ORDER BY p.Performance_in_Minutes
)
WHERE ROWNUM = 1

答案 1 :(得分:0)

你去吧

Select A.FName, A.LName 
       from Athelete A
Join   Participation_IND P
       on A.AtheleteID= p.AtheleID 
And
P.Performance_in_Minutes >=(Select max (performace_in_minutes) from Participation_IND)

由于

答案 2 :(得分:0)

这是一种方法:

SELECT a.FName
     , a.LName
     , a.sex
     , p.eventID
     , p.performance_in_minutes
  FROM ( SELECT i.eventID, g.sex, MIN(i.performance_in_minutes) AS fastest_pim
           FROM athlete g
           JOIN participation_ind i
             ON i.athleteID = g.athleteID
          WHERE i.eventID IN (13)            -- supply eventID here
          GROUP BY i.eventID, g.sex
       ) f
  JOIN participation_ind p
    ON p.eventID = f.eventID
   AND p.performance_in_minutes = f.fastest_pim
  JOIN athlete a
    ON a.athleteID = p.athleteID
   AND a.sex = f.sex

f 显示别名的内联视图获取每个eventID的最快时间,运动员表中每个“性别”最多一次。

我们使用JOIN操作从participant_ind中提取具有匹配的eventID和performance_in_minutes的行,并且我们使用另一个JOIN操作来获取来自Athle的匹配行。 (请注意,我们需要在“性别”列中包含连接谓词,因此我们不会无意中拉出具有匹配“分钟”值和不匹配性别的行。)

注意:如果有两个(或更多)同一性别的运动员对特定事件具有匹配的“最快时间”,则查询将拉出所有匹配的行;查询不区分每个事件的单个“最快”,它只区分具有特定事件“最快”时间的运动员。

可以省略/删除SELECT列表中的“额外”列。那些不是必需的;它们仅用于帮助调试。

答案 3 :(得分:0)

SELECT a.FName, a.LName
FROM ATHLETE a
WHERE
a.athleteID IN (
  SELECT mp.athleteID
  FROM PARTICIPATION_IND mp
  INNER JOIN ATHLETE ma
    ON mp.athleteID = ma.athleteID
  WHERE 
    mp.eventID = 13 AND
    ma.Sex = 'M'
  HAVING MAX(mp.Performance_in_minutes)

  UNION

  SELECT fp.athleteID
  FROM PARTICIPATION_IND fp
  INNER JOIN ATHLETE fa
    ON fp.athleteID = fa.athleteID
  WHERE 
    fp.eventID = 13 AND
    fa.Sex = 'F'
  HAVING MAX(fp.Performance_in_minutes)
);