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 7946057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:02:29+00:00 2026-06-04T01:02:29+00:00

I have a points system on my website and points are gained for different

  • 0

I have a points system on my website and points are gained for different achievements within the different website categories. Categories can have sub-categories and parent categories using the ‘category_relations’ table with ‘links_to’ and ‘links_from’ fields holding the relevant category_id’s.

What i’d like to do is fetch the top 5 users with the most points in the current category and any categories directly below it.

My ‘points_awarded’ table has all the records of any points awarded and to what users from what categories:

user_id,
points_amount,
plus (tinyint boolean if it’s points added or not),
minus (tinyint boolean if it’s point penalty or not),
category_id

I don’t really know where to start with this. Will i need to have two queries, one to fetch all sub category id’s, and then one to use that to run another query to fetch SUM() of the points used? Is it possible to do it in one query?

  • 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-06-04T01:02:30+00:00Added an answer on June 4, 2026 at 1:02 am

    I’d need more information about your database tables to be sure but something like this will probably work:

    SELECT 
        `user_id`, 
        SUM(IF(`plus`, `points_amount`, 0)) - SUM(IF(`minus`, `points_amount`, 0)) AS `points`
    FROM 
        `points_awarded`
    WHERE
        `user_id` = $user_id
        AND (
            `category_id` = $cat_id
            OR `category_id` IN(
                SELECT `links_to`
                FROM `category_relations`
                WHERE `links_from` = $cat_id
            )
        )
    GROUP BY `user_id`
    

    I’m curious though, why do you have a plus field and a minus field? If plus is false, can’t we assume it’s minus? Why have either field anyway, why not just make points_amount a signed field?

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

Sidebar

Related Questions

I have a CakePHP website with its own login system using the Auth component.
I have two points in a cathesian 2D system, both give me the start
i have two points in 3D space which have X-coordinates with different signum. so
I have 3 points ( lat , lon ) that form a triangle.How can
I have a windows application which is published on a website and can be
I am currently in the process of coding a points system for a website
I have created a <form> that points to http://localhost:17099/Form/Subscribe and hosted in my website...
I m trying to make feed system for my website, in which i have
I have a system in which users can register with my forum via the
Website using .NET Framework v3.5, SQL Server 2008, written in C# I have a

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.