我有三张桌子:

players(playerID: integer, firstName: string, lastName: string)  
gamestats(playerID: integer, gameID: integer, stat: string, result: double)  
games(playerID: integer, gameID: integer, gametype: string)

吉文斯:

  1. gamestats.stat中有两个不同的字符串:'hits'和'throws'
  2. gamestats.result列包含各自的成就结果。
  3. games.gametype列包含游戏类型,即。 'gameA','GameB'等

我想要的是返回以下内容:

SELECT SUM('hits')/SUM('throws') AS GameA_HitsPerThrow
WHERE games.gametype = 'gameA'
AND games.playerID = 1;

我知道这不是有效的SQL,但我希望psuedocode有意义。

任何帮助将不胜感激。 我是新手。

编辑:

样本数据:

player(1, 'John', 'D')
player(2, 'Alice', 'H')
gamestats(1, 1708, hits, 4)
gamestats(1, 1708, throws, 4)
gamestats(2, 1708, hits, 8)
gamestats(2, 1708, throws, 7)
gamestats(1, 1709, hits, 9)
gamestats(1, 1709, throws, 13)
gamestats(2, 1709, hits, 6)
gamestats(2, 1709, throws, 10)
games(1, 1708, 'gameA')
games(2, 1708, 'gameA')
games(1, 1709, 'gameA')
games(2, 1709, 'gameA')

这是我到目前为止所做的,但它只是返回查询中的所有行(尽管由于某种原因它也包含重复项):

SELECT gamestats.stat, gamestats.result FROM `gamestats`
INNER JOIN games 
ON gamestats.gameID = games.gameID
WHERE game.playerID = 1
AND games.gametype = 'gameA'
AND (gamestats.stat = 'hits' OR gamestats.stat = 'throws')

这是它返回的内容:

(name, result):  
(hits, 4)  
(throws, 4)  
(hits, 9)  
(throws, 13) 

这只是朝着我的目标迈出的一小步(非常小)。

===============>>#1 票数:2

你没有提到表名可能是这个原因问题

SELECT SUM('hits')/SUM('throws') AS GameA_HitsPerThrow From yourTable
WHERE games.gametype = 'gameA';

更多细节: 在Mysql查询中划分

===============>>#2 票数:0

SELECT g.gameID, g.playerID,
       SUM(CASE WHEN stat = 'hits' THEN result END)/
           SUM(CASE WHEN stat = 'throw' THEN result END) AS GameAHitsPerThrow
FROM games AS g
JOIN gamestats AS s ON g.gameID = s.gameID and g.playerID = s.playerID
WHERE g.gameType = 'gameA'
GROUP BY g.gameID, g.playerID

您有重复的行,因为您没有在JOIN条件中包含playerID 我不确定为什么你在每个玩家的games桌上都有单独的行。

  ask by copperhead4750 translate from so

未解决问题?本站智能推荐: