Help: How to have a play count and total records count?

Discussion in 'PHP' started by calcalmx123, Feb 18, 2014.

  1. #1
    Hey I have made a basic PHP script that holds flash game information and displays them.
    I want some site statistics such as total games, total plays. Please help.

    the play count is on each game in the 'game' table under the field 'plays'. I just need some way of adding all of the 'plays' from each game to get my total plays. As for the total games i just need something to count how many records there are in the 'game' table.

    Thanks,
    Callum.
     
    calcalmx123, Feb 18, 2014 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    SELECT SUM(plays) AS total_plays FROM game
    Code (markup):
     
    stephan2307, Feb 18, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    And for the count of all the games, just do
    
    SELECT COUNT(*) FROM game
    
    Code (markup):
     
    PoPSiCLe, Feb 18, 2014 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Total can be retrieved using group by modifiers
    Example:
    SELECT game_id,SUM(plays) FROM game
    GROUP BY game_id
    WITH ROLLUP;
    Code (markup):
    The grand total super-aggregate line is identified by the value NULL in the game_id column.
     
    koko5, Feb 18, 2014 IP