特定の国とスポーツの上位3名の選手のリストを返すにはどうすればいいですか?ランクに基づくSql結果の返信
ATHLETES
+------------+-----------------+---------------+-------------------------+
| idATHLETES | ATHLETENAME | TEAMS_idTEAMS | TEAMS_COUNTRY_idCOUNTRY |
+------------+-----------------+---------------+-------------------------+
| JG | JUSTIN GATLIN | USA-TF-MEN | USA |
| MS | MARIA SHARAPOVA | RUS-WTA | RUS |
| SW | SERENA WILLIAMS | USA-WTA | USA |
| UB | USAIN BOLT | JAM-TF-MEN | JAM |
| VW | VENUS WILLIAMS | USA-WTA | USA |
+------------+-----------------+---------------+-------------------------+
EVENTS
+------------+---------------+---------------------+------------------------+----------------------------------+--------------------------+
| idEVENTS | EVENTNAME | ATHLETES_idATHLETES | ATHLETES_TEAMS_idTEAMS | ATHLETES_TEAMS_COUNTRY_idCOUNTRY | VARIOUS_SPORTS_SPORTS_ID |
+------------+---------------+---------------------+------------------------+----------------------------------+--------------------------+
| ATH | ATHLETICS | JG | USA-TF-MEN | USA | TRACK-AND-FIELD |
| ATH | ATHLETICS | UB | JAM-TF-MEN | JAM | TRACK-AND-FIELD |
| TEN | TENNIS | MS | RUS-WTA | RUS | WOMENS_TENNIS |
| TEN | TENNIS | VW | USA-WTA | USA | WOMENS_TENNIS |
| TEN-DOUBLE | TENNIS DOUBLE | SW | USA-WTA | USA | WOMENS_TENNIS |
| TEN-DOUBLE | TENNIS DOUBLE | VW | USA-WTA | USA | WOMENS_TENNIS |
+------------+---------------+---------------------+------------------------+----------------------------------+--------------------------+
RESULTS
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+----------------------------------------
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+---------------------------------+
| idRESULTS | STATUS | MEDALS | EVENTS_idEVENTS | EVENTS_ATHLETES_idATHLETES | EVENTS_ATHLETES_TEAMS_idTEAMS | EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY | EVENTS_VARIOUS_SPORTS_SPORTS_ID |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+---------------------------------+
| results1 | DID-NOT-WIN | SILVER | TEN | MS | RUS-WTA | RUS | WOMENS_TENNIS |
| results1 | WON | GOLD | TEN | VW | USA-WTA | USA | WOMENS_TENNIS |
| results2 | DID-NOT-WIN | BRONZE | ATH | JG | USA-TF-MEN | USA | TRACK-AND-FIELD |
| results2 | WON | GOLD | ATH | UB | JAM-TF-MEN | JAM | TRACK-AND-FIELD |
| results3 | WON | GOLD | TEN-DOUBLE | SW | USA-WTA | USA | WOMENS_TENNIS |
| results3 | WON | GOLD | TEN-DOUBLE | VW | USA-WTA | USA | WOMENS_TENNIS |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+---------------------------------+
VARIOUS_SPORTS
+-----------------+----------------------+
| SPORTS_ID | SPORTS_NAME |
+-----------------+----------------------+
| TRACK-AND-FIELD | MENS TRACK AND FIELD |
| WOMENS_TENNIS | WOMENS TENNIS |
+-----------------+----------------------+
THE_COUNTRY
+-----------+-------------+
| idCOUNTRY | COUNTRYNAME |
+-----------+-------------+
| JAM | JAMAICA |
| RUS | RUSSIA |
| USA | USA |
+-----------+-------------+
THE_TEAMS
+------------+----------------------------------+-------------------+
| idTEAMS | TEAMNAME | COUNTRY_idCOUNTRY |
+------------+----------------------------------+-------------------+
| JAM-TF-MEN | jamaican track and field men | JAM |
| RUS-WTA | russian women tennis association | RUS |
| USA-TF-MEN | usa track and field men | USA |
| USA-WTA | usa womens tennis association | USA |
+------------+----------------------------------+-------------------+
私はこのコードをこれまで持っていますが、これは必要な出力を返しませんか?
SELECT idATHLETES, ATHLETENAME, TEAMS_COUNTRY_idCOUNTRY, COUNTRYNAME FROM
athletes
JOIN EVENTS ON idATHLETES = idEVENTS
JOIN teams ON TEAMS_idTEAMS =idTEAMS
JOIN country ON COUNTRY_idCOUNTRY = idCOUNTRY
JOIN RESULTS ON ATHLETES_idATHLETES = EVENTS_ATHLETES_idATHLETES
WHERE EVENTS_VARIOUS_SPORTS_SPORTS_ID = 'WOMENS_TENNIS' AND EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY ='USA' ;
これはアスリート、カントリー、スポーツのGROUP BYとメダルのCOUNTを必要とするので、ORDER BYとMySQLのLIMITを使ってトップ3を得ることができます。 – argoc
トップ3を獲得するには、ソートを追加する必要があります。しかし、どのように文字列である "メダル"を並べ替えることができますか?そのフィールドを整数にする方が良いです。 –
ゴールド3、シルバー2、ブロンズ1、無し0のメダルとポイントのテーブルを追加することはどうですか?その&SUM(ポイント)の参加を追加し、idAthletesでグループ化し、ポイントの降順の合計3 – PaulF