我有这样的数据:
Team | goal
a | 5
b | 8
a | 5
f | 8
c | 5
a | 3
g | 5
d | 8
a | 5
e | 4
a | 9
a | 8
b | 2
c | 3
f | 1
我想让select table像这样:
例如:
Team | sum(goal)
a | 14
b | 13
c | 12
d | 11
others | sum(outside limit 0,4)
所以我想列出他的总进球前4名球队
并将所有外部限制返回到一个名为“其他”的记录中(将前4名以外的其他球队的目标相加)
a,b,c,d尚未定义,请使用select , order, and limit to get TOP 4 Goal
答案 0 :(得分:0)
我不确定你的意思是“外限0,4”。但您可以使用Union
查询:
如下所示:
select Team,sum(goal) as goal from table where Team in('a','b','c','d') group by Team
union
select 'Others' as Team ,sum(goal) as goal table where Team not in('a','b','c','d')
group by 1
答案 1 :(得分:0)
选择前4名球队的查询将如下: -
select `Team`, sum(`goal`) as goals
from table_name
order by goals desc limit 4;
对于其他团队,那么top $ query将是这样的
select `Team`, sum(`goal`) as goals
from table_name
order by goals desc limit 4,10;
(我不知道你的队伍总数,所以我把10作为参考)
答案 2 :(得分:0)
联盟加入的两个查询将给出所需的结果:
( SELECT team AS team,SUM(goal) AS goals
FROM table
GROUP BY 1
LIMIT 4 )
UNION
( SELECT 'Other',SUM(other.goals) FROM
( SELECT team AS team,SUM(goal) AS goals
FROM table
GROUP BY 1
LIMIT 5,999999 ) other
)