0
我有一個數據庫期末考試,當我解決一些樣本問題時,我遇到了一些問題。SQL-INTERSECT with HAVING + MAX of SUM
我有兩個表之間的多對多關係。
Player PlayerTournament Tournament
------- -------------------- -------------------
pk id_player fk id_player pk id_tournament
name fk id_tournament name
rank year city
country victories court_surface
tournament_type
我要做的是:
1)。列出2016年在紅土錦標賽中贏得至少一場比賽但沒有參加任何草地錦標賽的球員(姓名和國家)。 2)。列出勝利數最多的球員(名字,國家,勝利總數)。
我在想是這樣的:
1. SELECT P.NAME, P.COUNTRY
FROM Player P INNER JOIN PlayerTournament PT
ON P.ID_PLAYER= PT.ID_PLAYER
INNER JOIN Tournament T
ON T.ID_TOURNAMENT= PT.ID_TOURNAMENT
WHERE T.COURT_SURFACE="clay"
GROUP BY (something)
HAVING SUM(PT.VICTORIES)>=1
INTERSECT
(same select and inner joins)
WHERE T.COURT_SURFACE="grass"
GROUP BY (something)
HAVING COUNT(ID_PLAYER)=0
2.SELECT P.NAME, P.COUNTRY, SUM(PT.VICTORIES)
FROM Player P INNER JOIN PlayerTournament PT
ON P.ID_PLAYER= PT.ID_PLAYER
GROUP BY ...
HAVING sum of victories = max sum of victories
我不知道如果我的方式思考的問題是正確的,我需要用「具有」語句幫助。