从3个表中选择

时间:2016-06-14 23:20:40

标签: mysql 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 |
+------------+---------------+---------------------+------------------------+----------------------------------+
| ATH        | ATHLETICS     | JG                  | USA-TF-MEN             | USA                              |
| ATH        | ATHLETICS     | UB                  | JAM-TF-MEN             | JAM                              |
| TEN        | TENNIS        | MS                  | RUS-WTA                | RUS                              |
| TEN        | TENNIS        | VW                  | USA-WTA                | USA                              |
| TEN-DOUBLE | TENNIS DOUBLE | SW                  | USA-WTA                | USA                              |
| TEN-DOUBLE | TENNIS DOUBLE | VW                  | USA-WTA                | USA                              |
+------------+---------------+---------------------+------------------------+----------------------------------+

RESULTS
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+
| idRESULTS | STATUS      | MEDALS | EVENTS_idEVENTS | EVENTS_ATHLETES_idATHLETES | EVENTS_ATHLETES_TEAMS_idTEAMS | EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+
| results1  | DID-NOT-WIN | SILVER | TEN             | MS                         | RUS-WTA                       | RUS                                     |
| results1  | WON         | GOLD   | TEN             | VW                         | USA-WTA                       | USA                                     |
| results2  | DID-NOT-WIN | BRONZE | ATH             | JG                         | USA-TF-MEN                    | USA                                     |
| results2  | WON         | GOLD   | ATH             | UB                         | JAM-TF-MEN                    | JAM                                     |
| results3  | WON         | GOLD   | TEN-DOUBLE      | SW                         | USA-WTA                       | USA                                     |
| results3  | WON         | GOLD   | TEN-DOUBLE      | VW                         | USA-WTA                       | USA                                     |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+

如何获得参加多个赛事的运动员名单

  1. 并且至少赢得了其中一个
  2. 并且没有赢得任何人
  3. 我已经提出了下面这段代码,但这会返回错误的输出?

    SELECT idATHLETES, ATHLETENAME, EVENTNAME FROM athletes
    JOIN EVENTS ON idATHLETES = ATHLETES_idATHLETES
    JOIN RESULTS ON events.ATHLETES_idATHLETES = RESULTS.EVENTS_ATHLETES_idATHLETES
    WHERE idEVENTS >=2 AND STATUS = 'WON'
    

2 个答案:

答案 0 :(得分:1)

您可以使用至少一次获取至少有两个事件和状态='赢'的记录。

SELECT idATHLETES, ATHLETENAME, EVENTNAME FROM athletes
JOIN EVENTS ON idATHLETES = ATHLETES_idATHLETES
JOIN RESULTS ON events.ATHLETES_idATHLETES = RESULTS.EVENTS_ATHLETES_idATHLETES AND RESULTS.Status = 'WON'
HAVING COUNT(idEVENTS) >=2 AND COUNT(STATUS) >= 1

答案 1 :(得分:1)

参加过多场比赛且至少赢过其中一场比赛的运动员:

select a.idATHLETES as id,a.ATHLETENAME as Name,count(*) as evtCount, 
SUM(CASE WHEN r.STATUS = 'WON' THEN 1 ELSE 0 END) as victoryCount 
from athletes a 
join  EVENTS e 
on e.ATHLETES_idATHLETES = a.idATHLETES and e.ATHLETES_TEAMS_idTEAMS = a.TEAMS_idTEAMS and e.ATHLETES_TEAMS_COUNTRY_idCOUNTRY = a.TEAMS_COUNTRY_idCOUNTRY 
join RESULTS r 
on r.EVENTS_idEVENTS=e.idEVENTS and r.EVENTS_ATHLETES_idATHLETES=e.ATHLETES_idATHLETES -- etc 
group by a.idATHLETES,a.ATHLETENAME 
having evtCount>1 and victoryCount>0 
order by a.idATHLETES,a.ATHLETENAME; 

<强>结果:

+----+----------------+----------+--------------+
| id | Name           | evtCount | victoryCount |
+----+----------------+----------+--------------+
| VW | VENUS WILLIAMS |        2 |            2 |
+----+----------------+----------+--------------+

参加过多场比赛但未获胜的运动员:

select a.idATHLETES as id,a.ATHLETENAME as Name,count(*) as evtCount, 
SUM(CASE WHEN r.STATUS = 'WON' THEN 1 ELSE 0 END) as victoryCount 
from athletes a 
join  EVENTS e 
on e.ATHLETES_idATHLETES = a.idATHLETES and e.ATHLETES_TEAMS_idTEAMS = a.TEAMS_idTEAMS and e.ATHLETES_TEAMS_COUNTRY_idCOUNTRY = a.TEAMS_COUNTRY_idCOUNTRY 
join RESULTS r 
on r.EVENTS_idEVENTS=e.idEVENTS and r.EVENTS_ATHLETES_idATHLETES=e.ATHLETES_idATHLETES -- etc 
group by a.idATHLETES,a.ATHLETENAME 
having evtCount>1 and victoryCount=0 
order by a.idATHLETES,a.ATHLETENAME; 

<强>结果:

没有返回任何行,您缺少该数据

备注

这只是group by with having和条件聚合(加上真正混乱的列名):p