首页 > 解决方案 > 检查我在表中的点(字段)总和是否大于表中所有其他人的点总和。很少有活动

问题描述

我创建了一个表格,代表几支球队的体育比赛。在每场比赛中,他们都有另一个表格,显示每个球员为他的球队得分多少。我试图通过编写脚本来获得每支球队的最佳球员。每支球队的最佳球员是在他的球队所有比赛中得分最多的球员。

我写了这个脚本,它没有返回任何东西:

select nickname,pname as bestplayer 
from player , points,team
where player.pid = points.pid and player.tid = team.tid 
group by nickname,pname
having sum(pscore) > all(select sum(pscore)
                         from points,player,team
                         where player.pid = points.pid and player.tid = team.tid 
                         );

出于某种原因,下面的脚本确实有效。谁能解释为什么?

select nickname,pname as bestplayer 
from player , points,team
where player.pid = points.pid and player.tid = team.tid 
group by nickname,pname
having sum(pscore) >= all(select count(pscore)
                         from points,player,team
                         where player.pid = points.pid and player.tid = team.tid 
                         );

*我已将内部查询中的总和替换为计数。

标签: postgresql

解决方案


推荐阅读