2016-11-09 75 views
-2

所以,我现在有一个表列出这样SQL查询前10胜

gameID | userID | WinLose 
1  | 1  | win 
2  | 1  | win 
3  | 1  | lose 
4  | 2  | win 
5  | 2  | lose 
6  | 2  | win 

它需要用户ID作为从用户表的外键,并记录每场比赛出场,与天气沿着用户的好评或丢失。

有谁能告诉我怎样编写一个查询,将每个用户赢得一次游戏的次数加起来,然后列出10个最高值?

我试图显示最多赢得前10名的用户。

谢谢你的帮助。

+2

SO是一个Q/A网站,而不是免费的编码服务。请提供您所尝试的内容以及您在尝试中遇到的问题。 – Shadow

+0

只有6结果 – Strawberry

+0

请参阅http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-bea-a-非常简单的SQL查询 – Strawberry

回答

1

您可以countgroup by条款试图让用户的总和,然后order by对结果进行排序,并获得最高10 limit

select userID 
    , count(*) as total_wins 
from tbl 
where WinLose='win' 
group by userID 
order by total_wins desc 
limit 10 

或有条件sum

select userID 
    , sum(WinLose='win') as total_wins 
from tbl 
group by userID 
order by total_wins desc 
limit 10 
+0

'sum'?为什么不计算(*)...哪里WinLose ='win''? –