2010-12-17 151 views
1

如何在sql中编写此查询: 对于每个玩过两个以上游戏的玩家,列出玩家姓名,总奖金和每位玩家玩的游戏数量。结果应由奖金由高到低进行排序如何在sql中编写此查询

和我的播放器表中的这些属性: playerId,playerName, 年龄和游戏桌这些attrubites: 游戏ID,playerId,结果记录结果attrubie充满无论是(第一或第二或第三或...,或没有显示)获胜者是谁有结果=第一个

这是我的弱点,我没有得到正确的答案,但是我所能做的就是这些。任何想法

select playerName,count(*),count(*) 
from games,player 
where games.playerId=player.playerId 
group by games.results 
+1

我没有看到与奖金相关的专栏。 – RedFilter 2010-12-17 15:36:21

+0

如果您提供了表格和结果集的模型,您希望看到我们可能会更好地帮助您。 – 2010-12-17 16:37:00

+0

确定结果如下:playerId-他赢得比赛多少次 - 他多少时间就像我说的那样简单:) – Stive 2010-12-17 16:45:26

回答

0

*试试这个(非常像你说的它的英文...
(如果“奖金”是在游戏中赢得的量),则:

Select playerName, count(*) Games, -- Number of game records per player 
     Sum(g.Winnings) Winnings  -- Sum of a Winnings attribute (dollars ??) 
    from player p Join Games g   -- from the two tables 
    On g.PlayerId = p.PlayerId  -- connected using PlayerId 
    Group by p.playerName    -- Output in one row per Player 
    Having Count(*) > 2     -- only show players w/more than 2 games 
    Order By Sum(g.Winnings)   -- sort the rows based on Player Winnings 

如果“奖金”你的意思的游戏赢得了数个恩...

Select playerName, Count(*) Games, -- Number of game records per player 
     Sum(Case g.WonTheGame   -- or whatever attribute is used 
      When 'Y' Then 1   -- to specify that player won 
      Else 0 End) Wins   -- Output in one row per Player 
    From player p Join Games g   -- from the two tables 
    On g.PlayerId = p.PlayerId  -- connected using PlayerId 
    Group by p.playerName    -- Output in one row per Player 
    Having Count(*) > 2    -- only show players w/more than 2 games 
    Order By Sum(Case g.WonTheGame  -- Sort by Number of games Won 
      When 'Y' Then 1 
      Else 0 End) 
+0

你能解释一下你写的是什么 – Stive 2010-12-17 16:43:04

+0

ok结果如下:playerId-他赢得比赛多少次 - 他的积分多少次就像我说的那样简单 – Stive 2010-12-17 16:46:16

+0

编辑添加评论 – 2010-12-17 18:35:43

3

你想看看GROUP BYHAVING连同COUNT。像这样的东西可能会做(未经测试):

SELECT 
    p.playerName 
    ,COUNT(g.*) 
    ,SUM(g.Winnings) -- you didn't name this column 
FROM 
    games g 
    INNER JOIN ON g.playerId = p.playerId 
WHERE 
    g.results = 1  -- whatever indicates this player was the winner 
GROUP BY 
    p.playerName 
HAVING 
    COUNT(g.*) > 2 
+0

但没有列奖金只有我有结果,如第一个和球员编号 – Stive 2010-12-17 16:08:44

+0

那么你是什么意思的“按奖金排序”?按赢得的比赛数量排序?如果是的话,放弃'SUM'并抛出一个带有'COUNT'子句的'ORDER BY'(如果你确认这种情况,我可以编辑它) – 2010-12-17 16:10:45

+0

但你可以通过遍历所有行来检索winer并查看谁在结果栏中有“第一”,然后带走每位选手,然后统计他有多少首先结果,这对我来说是一个问题。任何方式我喜欢你的答案,但如果你考虑我所说的这将是有益的:) – Stive 2010-12-17 16:11:29

0

这很难正是你从你的问题需要学到什么,但尝试这样的事:

select playerName, count(*) 
from games g 
join player p ON g.playerId = p.playerId 
group by playerName 
having count(*) > 2 
order by games.results DESC 
+0

这将仅返回玩家名称,但ineed他获胜的次数以及他所玩的游戏总数 – Stive 2010-12-17 16:16:28

+0

确定结果如下:playerId-他赢得多少次比赛 - 多少次他的发挥就像我说的那样简单 – Stive 2010-12-17 16:46:09

0

试试这个:

SELECT playerName, COUNT(g.PlayerID) as NumberOfPlays 
FROM games g ,player p 
WHERE g.playerId=p.playerId 
GROUP BY g.PlayerID 
HAVING COUNT(g.PlayerID) > 1 
ORDER BY g.results DESC 

选择 - 要显示
从数据 - 表
WHERE - 两个标识相互
GROUP匹配 - 游戏玩家ID,所以所有的计数都是正确的
HAVING - 确保他们玩的游戏多于一个游戏
ORDER BY - 以您想要的方式对结果进行排序米

+0

好的结果如下:playerId-他赢了多少次比赛 - 他的积分多少次就像我说的那样简单 – Stive 2010-12-17 16:46:01

0
select 
playerName, 
sum(if(games.result = 'first',1,0)) as wins, 
count(*) as gamesPlayed 
from player 
join games on games.playerId = player.playerId 
group by games.results 
having count(*) > 2 
order by count(*) desc;