MySql中的子查询问题

MySql中的子查询问题,mysql,Mysql,我在MySql中遇到了一个问题,请帮助我 在这个例子中,我有两个表,一个是一组竞争对手的结果,另一个是定义组成一个团队的三个竞争对手。事实上,我还有很多其他的表格,但它们并不是描述这个问题所需要的 Table with results for each competitor | competitor_id | result1 | result2 | result3 | result4 | | 1 | 1 | 1 | 1 | 1

我在MySql中遇到了一个问题,请帮助我

在这个例子中,我有两个表,一个是一组竞争对手的结果,另一个是定义组成一个团队的三个竞争对手。事实上,我还有很多其他的表格,但它们并不是描述这个问题所需要的

Table with results for each competitor
| competitor_id | result1 | result2 | result3 | result4 |
|       1       |    1    |    1    |    1    |     1   |
|       2       |    1    |    2    |    2    |     1   |
|       3       |    2    |    3    |    2    |     1   |
|       4       |    1    |    5    |    3    |     2   |
|       5       |    4    |    3    |    2    |     3   |
|       6       |    3    |    2    |    1    |     2   |
|       7       |    2    |    1    |    4    |     2   |
|       8       |    2    |    1    |    2    |     1   |
|       9       |    1    |    2    |    3    |     2   |

Table showing teams
| team_id | competitor1 | competitor3 | competitor3 |
|    1    |      1      |       3     |       4     |
|    2    |      2      |       8     |       9     |
|    3    |      7      |       6     |       5     |
现在,我想创建一个查询,给出每个团队的总数。我需要有一个查询(可能有子查询),因为我需要对总结果的desc进行排序

换句话说,我需要一个结果集,该结果集为每个团队的总结果提供team.id排序的desc

有人吗

编辑:这里有一个显示所需结果的更新

首先,让我们总结每个竞争对手的结果:

Competitor 1: 1+1+1+1=4
Competitor 2: 1+2+2+1=6
Competitor 3: 2+3+2+1=8
Competitor 4: 1+5+3+2=11
Competitor 5: 4+3+2+3=12
Competitor 6: 3+2+1+2=8
Competitor 7: 2+1+4+2=9
Competitor 8: 2+1+2+1=6
Competitor 9: 1+2+3+2=8
那么让我们看看团队表

Team 1 consists of competitors 1, 3 and 4.  
Team 2 consists of competitors 2, 8 and 9.  
Team 3 consists of competitors 7, 6 and 5.  

Total sum of team with id = 1 is 4+8+11=23  
Total sum of team with id = 2 is 6+6+8=20  
Total sum of team with id = 3 is 9+8+12=29  
考虑到所有这些,我希望我的结果集是

| id | team_sum |
| 3  |    29    |
| 1  |    23    |
| 2  |    20    |

为什么不重新设计数据库,就像只有两个表一样,一个用于竞争对手,另一个用于团队,比如:

Competitors Table:
`competitor_id`, `team_id`, `result1`, `result2`, `result3`, `result4`

Team Table:
`team_id`, `team_name`
您的查询将非常简单,如:

SELECT A.team_id, B.team_name, SUM(result1+result2+result3+result4) as TotalResult
FROM competitors A
INNER JOIN team B
ON A.team_id=B.team_id
GROUP BY A.team_id, B.team_name

请参见我的

每个团队的总数“您是指一个团队中的竞争对手参加的比赛总数还是一个团队中的竞争对手数量?尽管我怀疑是后者,因为您的团队表中有3列竞争对手。或者,您可能指的是每个组的总结果?您可以根据您的样本数据发布所需结果吗?可以重新设计您的数据库吗?例如,不需要有竞争对手的团队,而只需在团队表上显示TeamID和TeamName,然后在另一个表中显示竞争对手的CompetitorID和TeamID,这样最容易查询你的数据库。此外,在团队中添加或删除成员更为灵活。是的,欢迎提供详细的重新设计建议。如果您能给出使用该解决方案的任何查询示例,我将不胜感激。每当您看到带编号的列时,您都知道您遇到了麻烦-您的设计不是:创建一个子表来保存结果。