这是三个示例表
团队
+-------------+-------------+
| TeamID | TeamName |
+-------------+-------------+
| 11 | AA |
+-------------+-------------+
| 12 | BB |
+-------------+-------------+
| 13 | CC |
+-------------+-------------+
匹配
+-------------+
| MatchID |
+-------------+
| 1 |
+-------------+
| 2 |
+-------------+
| 3 |
+-------------+
团队比赛
+-------------+-------------+-------------+
| TmID | Team_ID | MatchID |
+-------------+-------------+-------------+
| 21 | 11 | 1 |
+-------------+-------------+-------------+
| 22 | 11 | 2 |
+-------------+-------------+-------------+
| 23 | 12 | 1 |
+-------------+-------------+-------------+
| 24 | 13 | 2 |
+-------------+-------------+-------------+
| 24 | 11 | 3 |
+-------------+-------------+-------------+
目标是列出TeamName
多次参加比赛的地方,如下所示:
+-------------+
| TeamName |
+-------------+
| AA |
+-------------+
这是我尝试过的:
SELECT TeamName
FROM Team T, Match M, Teammatch TM
WHERE T.TeamID = TM.TeamID AND
M.MatchID = TM.MatchID
......
但是我真的不知道下一步该怎么做。
我之前曾经使用SELECT COUNT
提出过类似的问题,但是我真的不明白如何在这里编写子查询。
答案 0 :(得分:0)
未经测试,但类似以下查询的查询应该起作用。按TeamID分组以使每个团队排成一行,然后按计数汇总MatchID以获取该团队的比赛次数。将显示的结果限制为具有该语句的HAVING语句。
SELECT T.TeamName
FROM TeamMatch TM
JOIN Match M ON M.MatchID = TM.MatchID
JOIN Team T ON t.TeamID = TM.TeamID
GROUP BY TeamID
HAVING COUNT(MatchID) > 1