Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:57:04+00:00 2026-05-31T19:57:04+00:00

I have a MySQL query that: gets data from three tables linked by unique

  • 0

I have a MySQL query that:

  • gets data from three tables linked by unique id’s.
  • counts the number of games played in each category, from each user
  • and counts the number of games each user has played that fall under the “fps” category

It seems to me that this code could be a lot smaller. How would I go about making this query smaller. http://sqlfiddle.com/#!2/6d211/1

Any help is appreciated even if you just give me links to check out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:57:06+00:00Added an answer on May 31, 2026 at 7:57 pm

    Generally it’s a good idea to have your join logic as part of the [Inner|Left] Join clause, rather than as part of the Where clause. In your case of simplifying the query, this cleans up your Where clause so that the query processor doesn’t apply filter conditions too early, which restricts what you want to do in more complex parts of the query (and impacts the overall performance of the query).

    By refactoring the join conditions, we can reduce the query to its core join across the three tables, and then add the join to the specialised subquery where the aggregation occurs. This results in only one nested query, which joins across the fewest tables needed.

    Here’s what I came up with:

    SELECT
        u.user_id
        ,pg.game_id
        ,u.user
        ,g.game
        ,g.game_cat
        ,ga.cat_count
        ,ga.fps_count
    FROM users u
    inner join played_games pg
        on u.user_id = pg.user_id
    inner join games g
        on pg.game_id = g.id
    inner join
    (
        select 
            ipg.user_id
            ,ig.game_cat
            ,count(ig.game) cat_count
            ,sum(case when ig.game_cat = 'fps' then 1 else 0 end) fps_count
        from played_games ipg
        inner join games ig
            on ipg.game_id = ig.id
        group by
            ipg.user_id
            ,ig.game_cat
    ) ga
        on g.game_cat = ga.game_cat
        and pg.user_id = ga.user_id
    order by
        ga.fps_count desc
        ,u.user
        ,ga.cat_count desc;
    

    One difference between the original query (apart from the slight rename) is that the fps_count field has a value of 0 instead of NULL for players who haven’t played a single FPS game. Hopefully this isn’t so critical, but rather helps to add meaning to the query.

    Lastly, I’m not sure about the context of how this is going to be used. In my opinion it’s probably trying to do too much in both listing every game played by every user (one objective) and summarising the categories of games played by each user (a separate objective). This means that the summary details are being repeated multiple times, e.g. for users playing multiple games of a particular category, which may not be ideal. My recommendation would be to separate these out into two separate queries, though I don’t know whether that would meet your specific needs.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I have a mysql query that gets some data. Then runs another
I have a MySQL query that fetches a numeric value VARCHAR (20) from a
I have a sub-query that just gets the sum of the number of items
I have a mysql query that targets a single column in a single row
I have a mysql query that uses union to join multiple queries into one
I have a mysql db query that I'd like to take and condense into
I have a PHP/MySQL query that returns to an HTML table, and I'm stuck
So I have this awesome MySQL query that's returning me an awesome array >
I'm running an MySQL query that returns results based on location. However I have
I am using in C# MYsql .I have query that works if I run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.