我正在试图弄清楚如何生成一个查询,以查看两个玩家在游戏中相互竞争的队长之间的胜率。
我在http://sqlfiddle.com/#!2/a2f8c/1
上的sqlfiddle上创建了架构表格(称为游戏)及其相关字段为:
gameID | pID | pWin | pLose | pCaptain
100 1 0 1 1
100 2 1 0 1
如果玩家赢了一场游戏,则pWin设置为1,pLose设置为0,而失败者将获得pWin中的0和pLose中的1。如果他们是挑选球队的队长,那么pCaptain将被设置为1。
我希望能够输入两个玩家ID并获得胜率。我一直在修补一些查询,但涉及的查询超出了我对SQL的了解。
我很感激任何帮助!
答案 0 :(得分:2)
像
这样的东西SELECT p1.sumwin/(p1.sumwin + p2.sumwin)*100 as p1,
p2.sumwin/(p1.sumwin + p2.sumwin)*100 as p2
FROM
(SELECT pID, sum(pWin) sumWin
FROM games
WHERE pCaptain = 1
GROUP BY pID) p1,
(SELECT pID, sum(pWin) sumWin
FROM games
WHERE pCaptain = 1
GROUP BY pID) p2
WHERE p1.pid = 1 AND
p2.pid = 2
或者我没有理解你(在那种情况下抱歉)。
@MotiveKyle现在好多了?
SELECT (sum(p1.pWin)/(sum(p1.pWin) + sum(p2.pWin)))*100 p1Win,
(sum(p2.pWin)/(sum(p1.pWin) + sum(p2.pWin)))*100 p2Win
FROM
(SELECT pID, pWin, gameID
FROM games
WHERE pCaptain = 1) p1,
(SELECT pID, pWin, gameID
FROM games
WHERE pCaptain = 1) p2
WHERE p1.gameID = p2.gameID AND
p1.pID = 1 AND
p2.pID = 2
基本上说: