在mysql中未获得所需的输出

在mysql中未获得所需的输出,mysql,sql,logic,Mysql,Sql,Logic,我在mysql中启动了一个查询,但没有得到所需的输出。 代码如下: select team_name, sum(semis.points+final.points) as final_points from semis inner join final on semis.sid=final.sid inner join teams on teams.tid=semis.tid group by semis.tid union select team_name,

我在mysql中启动了一个查询,但没有得到所需的输出。 代码如下:

select team_name,
       sum(semis.points+final.points) as final_points 
from semis 
inner join final on semis.sid=final.sid 
inner join teams on teams.tid=semis.tid 
group by semis.tid 
   union 
select team_name,
       semis.Points 
from semis 
inner join teams on semis.tid=teams.tid 
left join final on semis.sid=final.sid 
where final.sid is null;
输出:

+-----------------------+--------------+
| team_name             | final_points |
+-----------------------+--------------+
| BioTech & BioChem     |            7 |
| Chemistry             |            7 |
| Botany & Zoology      |            7 |
| Physics & Electronics |           17 |
| BCA                   |           19 |
| BCOM                  |           11 |
| Gujarati              |           10 |
| English               |           10 |
| Economics             |           20 |
| BCOM                  |            3 |
| Chemistry             |            3 |
| English               |            3 |
+-----------------------+--------------+
我想得到的结果是什么

+-----------------------+--------------+
| team_name             | final_points |
+-----------------------+--------------+
| BioTech & BioChem     |            7 |
| Chemistry             |           10 |
| Botany & Zoology      |            7 |
| Physics & Electronics |           17 |
| BCA                   |           19 |
| BCOM                  |           14 |
| Gujarati              |           10 |
| English               |           13 |
| Economics             |           20 |
+-----------------------+--------------+

将最后3个值添加到english、bcom、chemistry中,将其增加3,并从您发布的示例数据和预期结果中得出bcom:14、chemistry:10、english:13的总数。看起来您可以不使用
UNION
,通过左键加入
final
coalesce()
对于
最终分数

select team_name, sum(semis.points + coalesce(final.points, 0)) as final_points 
from semis 
inner join teams on teams.tid=semis.tid 
left join final on semis.sid=final.sid 
group by semis.tid 

将求和移动到外部查询,在那里可以在子查询中进行并集。欢迎使用SO。请参阅: