我目前正在尝试获取以下数据:
UserName,UserImageURL,已玩游戏总数,已完成游戏,已失去游戏,平均获胜(以百分比表示)和用户点数
另外还有一组数据:
用户统计数据,例如:
网站统计信息:
我有以下表格:
- 用户表 -
userID(int-pk),userName(varchar),userImageUrl(text)
-Games table -
gameId(int-pk),gameName(varchar),gameUserID(int),gameLeagueId(int),score1(int),score2(int),gameResultOut(0或1),gameWon(0或1)
-UserBalance table -
ubId(int-pk)userId(int)balance(int)
- 联合表 - leagueId(int-pk)leagueName(varchar)
只是为了让您了解正在发生的事情,当用户玩游戏并选择一些结果时,会在游戏表中插入一行。由于游戏是基于时间的,当结果结束时,有一个检查,检查是否有任何具有该id的游戏,并将根据用户选择的游戏将gameResultOut更新为1并将gameWon更新为1或0分数。
我尝试了以下内容:
SELECT u.userID, u.userName, u.userImageUrl, l.leagueName ,
COUNT(g.gameId) AS predTotal,
(SELECT COUNT(g.gameId) FROM games AS g WHERE g.gameResultOut = 1 AND g.gameWon = 1) AS gamesWon,
(SELECT COUNT(g.gameId) FROM games AS g WHERE g.gameResultOut = 1 AND g.gameWon = 0) AS gamesLost,
ub.balance
FROM games AS g
LEFT JOIN league AS l ON l.leagueId = g.gameLeagueId
LEFT JOIN user AS u ON u.user_id = g.gameUserID
LEFT JOIN user_balance AS ub ON ub.userId = u.userID
WHERE l.leagueId = 4
GROUP BY u.userId
ORDER BY ub.balance DESC
我可以轻松计算查询后的胜率,这不是问题,但Wins和Lost的结果都是一样的,即使更改leageId,结果仍然是相同的,不是我想要的是什么。
有人可以帮忙吗?
谢谢&问候, 的Necron
答案 0 :(得分:1)
据我所知,游戏表存储用户所玩的游戏。因此,为了了解每个用户播放/赢/输的游戏数量,您错过了游戏和用户之间子查询中的链接>
您的子查询是:
(SELECT COUNT(g.gameId ) FROM games AS g WHERE g.gameResultOut = 1 AND g.gameWon = 1) AS gamesWon,
(SELECT COUNT(g.gameId) FROM games AS g WHERE g.gameResultOut = 1 AND g.gameWon = 0) AS gamesLost,
他们应该是:
(SELECT COUNT(gw.gameId ) FROM games AS gw WHERE gw.gameResultOut = 1 AND gw.gameWon = 1 AND gw.gameUserID = u.user_id) AS gamesWon,
(SELECT COUNT(gl.gameId) FROM games AS gl WHERE gl.gameResultOut = 1 AND gl.gameWon = 0 AND gl.gameUserID = u.user_id) AS gamesLost,
我想这就是你要找的东西:)
编辑根据评论,添加用户和网站统计信息的提示:
对于这些信息,您需要执行几个不同的查询,因为大多数查询将按给定列对某些值和/或组进行求和,这不适合另一个查询。我会试着给你一些想法,这样你就可以开展工作了。
您提供的查询的上一个答案会计算次用户丢失/赢得任何游戏的次数,但不会在游戏之间区分这些数据。
所以,如果你想知道哪个游戏用户获胜最多,你应该有这样的东西:
SELECT
g.gameName,
-- How many times the user won per game
(SELECT COUNT(gw.gameId) FROM games gw WHERE gw.gameResultOut = 1 AND gw.gameWon = 1 AND gw.gameUserID = u.user_id) AS gamesWon,
-- How many times the user payed each game
COUNT(g.gameId) AS gamesPlayed,
-- The Win Ratio. This may need a little work on, depending on what you want.
-- Be aware that if a user played a game 1 time and won, it's ratio will be 1 (100%)
-- So maybe you'll want to add your own rule to determine which game should show up here
(gamesWon / gamesPlayed) AS winRatio
FROM
games g
INNER JOIN user u ON u.user_id = g.gameUserID
-- Groups and counts data based on games + users
GROUP BY g.gameId, u.user_id
-- Now you order by the win ratio
ORDER BY winRatio DESC
-- And get only the first result, which means the game the player has most wins.
LIMIT 1
对于丢失的游戏,它几乎是相同的查询,改变了所需的字段和数学。
以前的查询,除了你不再按游戏ID分组。只需按用户分组并进行数学运算。
嗯,据我所知,我们仍然在进行类似的查询。不同之处在于,对于整个站点统计信息,您不会按用户分组。你可以按照游戏或联赛进行分组,具体取决于你想要达到的目标。
底线:看起来大多数查询都相似,您必须使用它们并根据需要检索的每个信息进行调整。 请注意,它们可能无法正常工作,因为我无法在您的数据库上测试它们。您可能需要根据数据库/表模式更正某些不一致性。
我希望这可以为您提供一些有用的见解。