Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As far as I see, the <em>games</em> table stores games that <em>users</em> played. So, in order to know how many games <strong>each user</strong> played/won/lost, you're missing the link in the subqueries between <em>games</em> and <em>users</em>.</p> <p>Your subqueries are:</p> <pre><code>(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, </code></pre> <p>And they should be:</p> <pre><code>(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, </code></pre> <p>I guess this is what you're looking for :)</p> <hr> <p><strong>EDIT</strong> based on comments, adding tips for User and Site statistics:</p> <p>For those information you'll need to perform several distinct queries, as most of them are going to sum some values and/or group by a given column, which won't fit for another query. I'll try to give you some ideas so you can work on them.</p> <h1>User Statistics</h1> <h2>Most Games Won or Lost</h2> <p>The previous answer for the query you provided counts how many <em>times</em> user has lost/won <em>any</em> game, but does not distinct this data between games.</p> <p>So, if you want to know <em>in which</em> game user has most wins/losses, you should have something like this:</p> <pre><code>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 </code></pre> <p>For lost games, it's pretty much the same query, changing the desired fields and maths.</p> <h2>Game winning accuracy</h2> <p>Somewhat the previous query, except that you won't group by the gameID anymore. Just group by the user and do your math.</p> <h1>Site Statistics</h1> <p>Well, as far as I see, we're still on a similar query. The difference is that for the whole Site statistics you won't ever group by user. You may group by game or league, depending on what you are trying to achieve.</p> <hr> <p>Bottom line: looks that most queries are similar, you'll have to play with them and adapt for each information you need to retrieve. <strong>Please note that they might not work plenty</strong> as I could not test them on your DB. You may need to correct some inconsistence according to your database/tables schema. </p> <p>I hope this may give you some insight to work on. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload