0

I have 2 tables. One displays a game played (Date,Where, result,opponent etc) and the other one the details of a batting innings (runs scored, etc) Both tables have a primary key that relates the batting back to a specific game.

I am trying to return the OPPONENT column from Games when the MAX (highest) score is recorded in the table BATTING, but currently i am unsure how to do this.

The 2 tables can be found here

http://i.imgur.com/bqiyD3X.png

The example from these tables would be (max score is 101 in RUNSSCORED, so return the linked OPPONENT from GAMEINDEX which is "Ferndale"

Any help would be great. Thanks.

4

2 回答 2

1

Is this what you are looking for?

select OPPONENT 
from GAMES 
where GAMESINDEX in 
  (select GAMESINDEX from BATTING order by RUNSSCORED desc limit 1);

If there isn't a unique max RUNSSCORED value, then the answer might not be deterministic.

If you want multiple winners in that case, you could use

select OPPONENT  
from GAMES natural join BATTING 
WHERE RUNSSCORED in (select MAX(RUNSSCORED) from BATTING);
于 2013-10-20T08:40:27.497 回答
0
SELECT G.OPPONENT, MAX(B.RUNSSCORED)
FROM GAMES AS G 
     INNER JOIN BATTING AS B
     ON G.GAMESINDEX = B.GAMESINDEX
于 2013-10-20T08:57:03.953 回答